Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a conditional lock in C#?

The thing is I've been using the lock statement to protect a critical part of my code, but now, I realize I could allow concurrent execution of that critical code is some conditions are met.
Is there a way to condition the lock?

like image 203
sebagomez Avatar asked Oct 28 '08 12:10

sebagomez


People also ask

What is lock in condition?

Terms in an agreement prescribing for a time period within which any one of the contracting parties or either of the contracting parties cannot terminate the contract is known as a lock-in clause.

How do you create a condition variable?

Creating and destroying condition variables A condition variable is created by calling the pthread_cond_init subroutine. You may specify a condition attributes object. If you specify a NULL pointer, the condition variable will have the default attributes.

What is a lock and condition variable?

Condition variables enable threads to atomically release a lock and enter the sleeping state. They can be used with critical sections or slim reader/writer (SRW) locks. Condition variables support operations that "wake one" or "wake all" waiting threads.

How do you create a condition on a lock Java?

You can create a condition variable by calling lock. newCondtion() method. This class provides a method to wait on a condition and notify waiting for threads, much like the Object class' wait and notify method. Here instead of using wait() to wait on a condition, you call await() method.


1 Answers

I think that question cries "race condition!". What if the condition turns from true to false shortly after the check, but before a thread enters the critical section of code? Or while a thread is in the process of executing it?

like image 59
Tomalak Avatar answered Oct 28 '22 15:10

Tomalak