Critical Section and Recursive Calls

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Critical Section and Recursive Calls

African Wild Dog
Hello,

I'm writing a function protected by a critical section which can have recursive calls under certain conditions.

Using the TCriticalSection class, after my thread acquires a critical section, if my thread make 6 additional calls to TCriticalSection.Enter, to release the critical section my thread will have to make 6 additional calls to TCriticalSection.Leave?

If so, is this behaviour the same on all platforms?

From the Windows API:

"After a thread has ownership of a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns. The thread enters the critical section each time EnterCriticalSection and TryEnterCriticalSection succeed. A thread must call LeaveCriticalSection once for each time that it entered the critical section."


Regards

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Critical Section and Recursive Calls

Mattias Gaertner
On Thu, 30 Mar 2017 03:36:02 -0300
African Wild Dog <[hidden email]> wrote:

> Hello,
>
> I'm writing a function protected by a critical section which can have
> recursive calls under certain conditions.
>
> Using the TCriticalSection class, after my thread acquires a critical
> section, if my thread make 6 additional calls to TCriticalSection.Enter, to
> release the critical section my thread will have to make 6 additional calls
> to TCriticalSection.Leave?

Yes.

 
> If so, is this behaviour the same on all platforms?

Yes.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal