[Lazarus] Strange dependency of units

Sven Barth pascaldragon at googlemail.com
Thu Apr 5 18:17:11 CEST 2012


Am 05.04.2012 15:20, schrieb Michael Schnell:
> On 04/05/2012 02:37 PM, michael.vancanneyt at wisa.be wrote:
>>
>>
>> TThread.Synchronize works perfectly in any kind of application outside of
>> the LCL.
>>
>> The main thread just has to call CheckSynchronize at regular intervals.
>> (which is what the LCL does for you).
>>
> Of course I do know this.
>
> But "regular intervals" (aka polling) is not a good idea. For a decent
> application (e.g. embedded stuff on a low performance hardware) you need
> to do a decent event scheduling. This means that the main thread needs
> to go to sleep via an OS API call (to completely free the CPU for other
> threads or external tasks), and that the sleeping main thread needs to
> be woken up (by means of en OS API call) at once if an event (may same
> be thrown by a timer, a Thread or a GUI action) (to allow for the lowest
> possible latency).

If you can accept a main thread that only handles Synchronize events 
then you can call CheckSynchronize with a very high timeout value (on 
Windows you can also use INFINITE=DWord(-1)). CheckSynchronize 
internally (platform independently!) waits for an event that is set once 
Synchronize is called (this event is reset after CheckSynchronize is 
done). So you basically utilize the operating system functionality.

Please note that only on Windows you get a true infinite wait while on 
systems based on Posix threads always a timeout wait is used (this could 
be circumvented if for "DWord(-1)" the non-timeout wait variant of the 
RTL would be used, but I don't know the potiential implications that 
such a change may have - though I also don't know how exactly 
CheckSynchronize will behave on Posix systems if you call it with 
INFINITE as argument).

Regards,
Sven





More information about the Lazarus mailing list