pthread
library, you do create and use a condition variable like this:
pthread_cond_t count_threshold_cv; pthread_mutex_t count_mutex; pthread_cond_wait(&count_threthold_cv, &count_mutex); // atomically unlock mutex while waiting phtread_cond_signal(&count_threthold_cv);When locks and condition variables are used together, the result is called a monitor:
- A collection of procedures manipulating a shared data structure.
- One lock must be held whenever accessing the shared data
- One or more condition variables used for waiting.
In C#, there is no separate type for the condition variable. Instead every object inherently implements one condition variable, and the "Monitor" class provides static "Wait", "Pluse" and "PulseAll" methods to manipulate an object's condition variable. They are used together with
In C# every object also inherently implements a mutual exclusion lock.
lock
or Monitor.Entor
and Monitor.Exit.
public static KV GetFromList() { KV res; lock (typeof(KV)) { while (head == null) Monitor.Wait(typeof(KV)); res = head; head = res.next; res.next = null; // for cleanliness } return res; }
In C# every object also inherently implements a mutual exclusion lock.
No comments :
Post a Comment