[Lazarus] How does one add an event to a blocking system?

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Wed Oct 21 16:19:24 CEST 2015


Michael Schnell wrote:
> On 10/21/2015 02:42 PM, Michael Schnell wrote:
>> CheckSynchronize()
>>
> Unfortunately there is no decent documentation on CheckSynchronize().
> 
> So in very short (and just hopefully correct):
> 
> CheckSynchronize() needs to be called by the main thread (in fact a 
> threat becomes the main thread _because_ it calls CheckSynchronize().
> 
> The Events thrown by other Threads via TThread.Synchronize and 
> TThread.Queue are executed as sub-procedures of CheckSynchronize() and 
> hence they are executed in the main thread and they are executed in the 
> order they have been queued by other threads.
> 
> It is a very bad idea to do a closed loop such as
> 
> while (TRUE) CheckSynchronize();
> 
> As this will use 100 % CPU time and hence will reduce the performance of 
> all other threads (and every application that is running on the processor).
> 
> That is why CheckSynchronize() provides using the parameter "Timeout", 
> that defines how long to wait until returning in case no event is 
> queued.  (0 or no parameter => return immediately)
> 
> AFAIR if an Event is in the queue same is executed and 
> CheckSynchronize(I) returns without checking if more Events are queued.
> 
> AFAIK the return parameter of CheckSynchronize() does not give any 
> decent result and needs to be ignored.

Seems reasonable. I think I've used it in one place, which is a console 
program which spends most of its time waiting for keyboard input but 
also has

procedure MainThreadYield;

begin
   Assert(InterlockedExchange(mainThreadYieldEntryCounter, 1) = 0, 
'MainThreadYield reentered');
{$ifndef LCL }
   CheckSynchronize;
{$endif LCL  }
   Assert(InterlockedExchange(mainThreadYieldEntryCounter, 0) = 1, 
'MainThreadYield reexited')
end { MainThreadYield } ;

I don't know whether I've overlooked anything significant there, but it 
appears robust and activity triggered by around half a dozen background 
threads appears as expected.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]




More information about the Lazarus mailing list