[Lazarus] Are multiple critical sections allowed in form or data module?

Hans-Peter Diettrich DrDiettrich1 at aol.com
Sun Oct 9 10:49:31 CEST 2011


Frank Church schrieb:

>      > Is it acceptable to have additional TCriticalSections on the form?
> 
>     Yes.
>     I hope you are aware that all code accessing the LCL must run in the
>     main thread.
> 
>     Mattias
> 
> 
> Can you explain this?

A critical section blocks all code, that wants to enter the same 
(occupied) critical section, until the current owner left the critical 
section. You can have any number of critical section objects, and they 
can be used in a nested manor - at the risk of deadlocks.

> Do you mean that TCriticalSection is part of the LCL and shouldn't be 
> run from within a thread, or that code within the thread or the critical 
> section must not run code accessing the LCL?

The LCL is not reentrant. All code accessing the LCL should run in the 
main thread (see Synchronize...), not in other threads.

> How is it determined whether a piece of code runs within the LCL or not?

Wrong question :-(

Whenever your code accesses a procedure or object of the LCL, that code 
should not reside (and execute) in an thread. A thread should provide a 
(callback) method instead, which contains such code, and make that 
method execute in the main thread (see Synchronize...).

I'm not sure how some code can determine, whether it runs in the main 
thread.

DoDi





More information about the Lazarus mailing list