tags:

views:

66

answers:

4

If a critical section lock is currently owned by a thread and other threads are trying to own this very lock, then all the threads enter into a wait queue. When the initial owning thread releases the criticakl section lock, then one of the threads in the waiting queue owns the thread. kindly if i am told how the owner is decided as it is not guranteed that the thread that first came will be the owner of the thread.

if threads are not served in FIFO fashion then how the owner Thread from the queue is picked.

+2  A: 

The next thread to get the critical section is chosen non-deterministically. The only thing that you should be concerned about is whether the critical section is implemented fairly, i.e., that no thread waits infinitely long to get its turn. If you need to run threads in specific order, you have to implement this yourself.

zvrba
what do you non-deterministically.Isn't there any algo implemented.kindly if you could explain non-deterministically.
Non-deterministic is for all practical purposes "random" in this context.
zvrba
Actually: why do you ask? If you are just curious about internal workings, read a book about operating system design. If you are desinging an application, you MUST NOT rely on any particluar algorithm that CS uses to wake up threads.
zvrba
Actually I am implementing a waiting threads vector and want to remove the lock ownership gaining thread from the vector.
You should have asked that question then, really. Simple solution though: Let the thread that gains the critical section remove itself.
MSalters
+1  A: 

The next thread is chosen in quasi FIFO order. However many system level variables may cause this to appear non deterministic:

From Concurrent Programming On Windows by Joe Duffy: (Chapter 5)

... When a fixed number of threads needs to be awakened, the OS uses a semi-fair algorithm to choose between them: as threads wait they are placed in a FIFO queue that the awakening logic consults when determining which thread to wake up. Threads that have been waiting for the longest time are thus preferred over threads that been waiting less time. Although the OS does use a strict FIFO data structure to manage wait lists; ... this ordering is regularly perturbed by other system code and is not reliable.

Preet Sangha
A: 

Posix threads do the FIFO queue.

hlynur
A: 

What about Thread Scheduling Algorithm , the threads in waiting state get priority as per Thread Scheduling algorithm Plz correct if I am wrong.

ronan