Recent

Author Topic: mtprocs work in console but not windows xp  (Read 5330 times)

sum

  • New member
  • *
  • Posts: 7
mtprocs work in console but not windows xp
« on: December 26, 2011, 03:17:07 pm »
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

  • Hero Member
  • *****
  • Posts: 1173
Re: mtprocs work in console but not windows xp
« Reply #1 on: December 27, 2011, 02:12:39 pm »
Quote
I do not know why console program can use all my 2 cores.
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

  • New member
  • *
  • Posts: 7
Re: mtprocs work in console but not windows xp
« Reply #2 on: December 27, 2011, 10:42:47 pm »
Do that mean I need convert my program to TThread ?

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: mtprocs work in console but not windows xp
« Reply #3 on: December 28, 2011, 08:34:48 am »
Do that mean I need convert my program to TThread ?
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

  • New member
  • *
  • Posts: 7
Re: mtprocs work in console but not windows xp
« Reply #4 on: December 30, 2011, 02:28:05 am »
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.


 

TinyPortal © 2005-2018