C ++ 11 output std :: mutex blocks a blocked thread into a passive wait state?

I have the following situation:

Two C ++ 11 threads are working on computation, and they are synchronized via std :: mutex.

Thread A locks the mutex until the data is ready to execute Thread B. When the mutex is unlocked, B. starts to work.

Thread B attempts to lock the mutex and locks until it is unlocked by thread A.

void ThreadA (std::mutex* mtx, char* data)
{
    mtx->lock();
    //do something useful with data
    mtx->unlock();
}

void ThreadB (std::mutex* mtx, char* data)
{
    mtx->lock(); //wait until Thread A is ready
    //do something useful with data
    //.....
}

It is argued that Thread A may first lock the mutex.

Now I wonder if mtx->lock()Thread B will be active or passive. Thus, Thread B checks the status of the mutex and spends processor time or is allocated passively using the scheduler when the mutex is unlocked.

++ , , .

, , std::mutex plattform ?

+4
1

, V++, Visual Studio 2010, std::mutex Win32 CRITICAL_SECTION. EnterCriticalSection(CRITICAL_SECTION*) : CRITICAL_SECTION, . , , , , , . , .

Visual Studio 2012 . std::mutex Win32. Win32 . , .

: std:: mutex win32 CRITICAL_SECTION

, , . .

. , std::lock_guard. , condition_variable .

+8

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


All Articles