jeudi 24 octobre 2019

Why is the usage of mutex correct in C++?

I was asked a question about mutex before, the code works fine, but I am confused about this result:

::std::mutex s_mutex;
void funcA()
{
    s_mutex.lock();

    printf( "funcA \n" );

    s_mutex.unlock();
}

void funcB()
{
    s_mutex.lock();

    funcA();

    printf( "funcB \n" );

    s_mutex.unlock();
}

int main()
{
    funcB();

    return 0;
}

It works fine and print what I did not etxpect:

funcA 
funcB 

But why? This func is called in main thread and seems to call lock twice.

Aucun commentaire:

Enregistrer un commentaire