State Variable - Pending / Notification of Race Status

I will talk about some code because the explanation is simpler. Suppose mutexes are used correctly with condition variables so that they are simple:

// Thread 1
while(1)
{
    conditionVariable.wait();
    // Do some work
}

// Thread 2
while(1)
{
    // Do some work
    conditionVariable.notify_one();
}

// Thread 3
while(1)
{
    // Do some work
    conditionVariable.notify_one();
}

What I would like to achieve is that stream 1 is expected to expect a condition variable when stream 2 or stream 3 is notified. As the code stands, there is a big gap between notify_one()and wait()in the form of some other code marked with comments. This gap means that it is sometimes called notify_one()before it is possible to call wait().

, , notify_one() wait() ( 1). , , (1 ) wait(), Threads 2 3 notify_one() , Thread 1 wait(). , .

wait(), , . , , wait() , , . , .

: , 1 , 2 3 ?

+4
1

: , - , .

, ( : ), .

, , 1 , , , , .

, ( conditionVariable ):

// Thread 1
while(1)
{
    lockReady();
    ready = true;
    unlockReady();
    readyCV.notify_one();
    conditionVariable.wait();

    // Do some work
}

// Thread 2
while(1)
{
    lockReady();
    while (! ready) readyCV.wait();
    ready = false;
    unlockReady();
    // Do some work
    conditionVariable.notify_one();
}

// Thread 3
while(1)
{
    lockReady();
    while (! ready) readyCV.wait();
    ready = false;
    unlockReady();
    // Do some work
    conditionVariable.notify_one();
}

.

+4

Source: https://habr.com/ru/post/1523291/


All Articles