Forum > Packages and Libraries

mtprocs work in console but not windows xp

(1/1)

sum:
I am using lazarus-0.9.30.2RC1 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:http://www.mediafire.com/file/bo8b3vta85ca1or/consoletest.zip
gui program:http://www.mediafire.com/file/tbfphpyyxp3zpva/guitest.zip

Thanks for help.

ludob:

--- 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 http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial. 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 http://wiki.lazarus.freepascal.org/Parallel_procedures#Synchronize on  how to use the synchronize procedure.

sum:
Do that mean I need convert my program to TThread ?

ludob:

--- 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.

sum:
I take a search 'synchronize' from google and find that
http://www.lazarus.freepascal.org/index.php?topic=7779.0
It said that I must "Synchronize works for MTProcs just like for any other TThread.
Synchronize requires that the main thread still runs.
Solution:
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.

Navigation

[0] Message Index

Go to full version