lundi 5 octobre 2015

Leads a C++11 std::mutex lock the blocked thread into a passive wait state?

I have the following situation:

Two C++11 threads are working on a calculation and they are synchronized through a std::mutex.

Thread A locks the mutex until the data is ready for the operation Thread B executes. When the mutex is unlocked Thread B starts to work.

Thread B tries to lock the mutex and is blocked 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 asserted that Thread A can block the mutex first.

Now I am wondering if the mtx->lock() in Thread B waits active or passive. So is Thread B polling the mutex state and wasting processor time or is released passively by the sheduler when the mutex is unlocked.

In the different C++ references it is only mentioned that the thread is blocked, but not in which way.

Could it be, however, that the std::mutex implementation is hardly depended on the used plattform and OS?

Aucun commentaire:

Enregistrer un commentaire