[Lazarus] PostMessage return value

Xiangrong Fang xrfang at gmail.com
Mon Sep 15 15:57:40 CEST 2014

2014-09-15 21:44 GMT+08:00 Michael Schnell <mschnell at lumino.de>:

> Performance wise it's better not to destroy and create your threads but to
> manage a thread pool and assign work to the threads as appropriate.
> Here you would have the threads wait for work  e.g. by trapping themselves
> in a TCriticalSection for each one.
> Now the Main thread can easily free a thread after it assigned work to it
> (e.g. by providing a callback function that is to be called in
> TThread.Execute). If work is done, the Thread could notify the manager (in
> the main thread) by Application.QueueAsyncCall or TThread.Queue. TThread.
> Synchronize would not harm in this case either, as the thread at that time
> has nothing to do anyway.

That's exactly what I planed to do. But I don't know "CriticalSections" can
be used in here.  My original thinking is that, by the end of Execute, I
call either PostMessage or QueueAsyncCall to notify main thread that the
current thread finished its work.  The main thread, in its message handler
then know from a status variable that this thread is in suspended status,
then assign a new work to it and call its Execute() method again.

Is that ok? To be exact, when a thread's execute() finished, it will go to
a "sleep" status, and can be reused by calling execute() again, right?

Even this works, if it is performance-wise not optimal, please give me a
short example of how to use CriticalSection for this purpose.

Thanks a lot.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20140915/8f2e37ed/attachment-0003.html>

More information about the Lazarus mailing list