Con Currency
Con Currency
S.Venkatesan
C Language
•OpenMP is simpler and provides automatic handling of parallelism.
•pthreads gives you low-level control, but requires more effort to manage threads manually.
Example
• One of the most commonly used programs is
Web browsers, whose design is based heavily on
concurrency.
Task A: TOTAL += 1
Task B: TOTAL *= 2.
Possession
• One general method for providing mutually exclusive access (to
support competition synchronization) to a shared resource - a task
can possess and allow only a single task to possess it at a time.
• Task scheduling
• So, a guard can be used to allow only one task to access a particular
shared data structure at a time.
• The queue of emptyspots can store producer tasks that are waiting
for available positions in the buffer;
• the queue of fullspots can store consumer tasks waiting for values
to be placed in the buffer.
Cooperation
semaphore fullspots, emptyspots;
fullspots.count = 0;
emptyspots.count = BUFLEN;
task producer;
loop
-- produce VALUE –
wait(emptyspots); { wait for a space }
DEPOSIT(VALUE);
release(fullspots); { increase filled spaces }
end loop;
end producer;
task consumer;
loop
wait(fullspots); { make sure it is not empty }
FETCH(VALUE);
release(emptyspots); { increase empty spaces }
-- consume VALUE –
end loop
end consumer;
Competition
• The wait statement allows the access only if the
semaphore’s counter has the value 1, which
indicates that the shared buffer is not currently
being accessed.
• However, if task A is waiting for a message at the time task B sends that message,
the message can be transmitted. This actual transmission of the message is called
a rendezvous.
• Note that a rendezvous can occur only if both the sender and receiver want it to
happen.