Basic Mechanisms Of Flow Synchronization
Modern OSes provide a wide range of ready-made synchronization
mechanisms.
Synchronization mechanisms are operating system tools that
help to solve the main task of synchronization - to ensure the coordination
of flows that work with shared data. If such tools are the minimum blocks
for building multithreaded programs,
they are called synchronization
primitives.
Synchronization primitives are divided into the following main categories:
❖
Versatile, low-level, which can be used in different ways (
semaphores )
❖
Simple, low-level, each of which is adapted to the solution
❖
High-level universal expressed through simple; this group includes
the
concept of a monitor which can be expressed through mutexes
and conditional variables
❖
High level, adapted to the solution of a specific synchronization
problem (blocking read-write and barriers)
Let us look at the different mechanisms and evaluate the advantages that
each of them has, as well as their possible disadvantages. To further
illustrate
the material, let's take a classic example that demonstrates the
need for synchronization - the problem of producer-consumers or the
problem of limited buffer ( bounded buffer ).
The formulation of the problem is simple. Suppose we have production
threads and consumer threads. The
manufacturer creates objects, the
consumer receives them. The task is to synchronize their work so that the
consumer cannot attempt to retrieve objects that have not yet been created,
and the manufacturer cannot create more objects than the consumer can.
To synchronize flows, place a buffer of fixed length
n between the
manufacturer and the consumer. The manufacturer can place objects on the
buffer, the consumer - take them from there.
If the consumer takes the
object, it is removed from the buffer. There are several requirements to be
met:
1. When the manufacturer or consumer is working with the
buffer, the rest of the threads should wait for it to finish
2. When the manufacturer is trying to put an about object in
the buffer and the buffer is full, it must wait until it appears
in place
3. When a consumer tries to pick an about object of the buffer
and the buffer is empty, it must wait until it is on ' appears
about '
object