[Lazarus] PostMessage return value

Michael Schnell mschnell at lumino.de
Mon Sep 15 15:44:58 CEST 2014

On 09/15/2014 02:55 PM, Xiangrong Fang wrote:
> How do I use Event to achieve the same?
What do you mean by "Event" ?
> It seems that I can have N threads "listen" to the same event, but 
> cannot have the main thread to "listen" to N different events?
In fact Windows does provide event (aka "Message") queues for all 
threads out of the boy, Linux does not provide any event queues.

Lazarus (like Delphi) provides some support for an Event Queue for the 
Main thread. Lazarus does this both in Windows and Linux (and Mac OS). 
The queue can be fed by GUI events, (non-Delphi) 
Application.QueueAsyncCall (and TThread.Synchronize and - with the FPC 
svn version - TThread.Queue).

There is no support by the language for Event Queues for worker Threads. 
In Windows you could use messages directly by API calls, in Linux you 
could use Pipes with the appropriate API calls like "open", "select", 
"epoll", ...

>  For now, I would like to know, performance-wise, which way is better? 
> Using QueueAsyncCall/PostMessage or RTLEvent*?

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.

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

More information about the Lazarus mailing list