Semaphores/Creating a Critical Section

157 views Asked by At

How can you use a semaphore to create a special critical section that allows two threads to be executing inside instead of the usual one thread?

1

There are 1 answers

0
jev On

In pseudocode it looks like so:

s = Semaphore(2)    # max 2 possible threads accessing the critical section

Each thread then uses the semaphore to serialize access:

s.decrement()    # may block 
    # enter critical section
s.increment()

Useful resource is: The Little Book of Semaphores