Forum > Packages and Libraries

mtprocs work in console but not windows xp


I am using lazarus- with window xp(inside vmware)

console program and gui program work same (think a Reversi game 's score with meaningless evaluation with same number of thread).

I do not know why console program can use all my 2 cores.
But gui program only use 1 core 's power.

console program:
gui program:

Thanks for help.


--- Quote ---I do not know why console program can use all my 2 cores.
--- End quote ---
Normally all interactions with the GUI should be done from the main thread. See What happens in your app is that every thread starts with a memo1.lines.add(inttostr(Index));. Adding a line to TMemo calls SendMessage(FHandle, EM_GETLINECOUNT, 0, 0) and Sendmessage waits until the message is dealt with. The message loop is in the main thread but as soon as the first thread starts running, the main thread doesn't become active any more. You can notice this because your window isn't repainted anymore. When the blocking thread finishes, the next one gets unlocked from SendMessage, starts running and will block in its turn the next one. In the end only one thread runs at the time.
Remove the memo1.lines.add(inttostr(Index)) and all cpu's will max out but your GUI will still be unresponsive. See on  how to use the synchronize procedure.

Do that mean I need convert my program to TThread ?


--- Quote from: sum on December 27, 2011, 10:42:47 pm ---Do that mean I need convert my program to TThread ?

--- End quote ---
No. mtprocs uses tthread in the background. Basically it adds the pooling to it in an effort to optimise multi-cpu/core systems.
The last wiki page I referred to is dealing with mtprocs and explains how to use 'synchronize' which is a method of TThread.
Important is that the threads leave some time to the main thread to handle messages.Since yçu are not doing any IO (disk,network,..) in the thread you'll need to add from time to time fe. a call to Application.Processmessages in a method called through synchronize.

I take a search 'synchronize' from google and find that
It said that I must "Synchronize works for MTProcs just like for any other TThread.
Synchronize requires that the main thread still runs.
Create a worker thread and execute the DoParallel there."
When I finish the "unuser freindly" version of my program,then I will convert to Tthread.


[0] Message Index

Go to full version