jeudi 25 octobre 2018

C++ program using mutex gives unpredictable output. Expecting a deadlock but don't get it

I have code using mutex for self learning. Link is : https://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-variables.html

I wrote the example: main_deadlock.cpp

#include <iostream>
#include <thread>
#include <mutex>

struct Complex {
    std::mutex mutex;
    int i;

    Complex() : i(0) {}

    void mul(int x){
        std::cout << "mul : before lock_guard" << std::endl;
        std::lock_guard<std::mutex> lock(mutex);
        std::cout << "mul : after lock_guard, before operation" << std::endl;
        i *= x;
        std::cout << "mul : after operation" << std::endl;
    }

    void div(int x){
        std::cout << "div : before lock_guard" << std::endl;
        std::lock_guard<std::mutex> lock(mutex);
        std::cout << "div : after lock_guard, before operation" << std::endl;
        i /= x;
        std::cout << "div : after operation" << std::endl;
    }

    void both(int x, int y)
    {
        std::cout << "both : before lock_guard" << std::endl;
        std::lock_guard<std::mutex> lock(mutex);
        std::cout << "both : after lock_guard, before mul()" << std::endl;
        mul(x);
        std::cout << "both : after mul(), before div()" << std::endl;
        div(y);
        std::cout << "both : after div()" << std::endl;
    }

};

int main(){

    std::cout << "main : starting" << std::endl;
    Complex complex;
    std::cout << "main : calling both()" << std::endl;
    complex.both(32, 23);

    return 0;
}

I would expect this code will have a deadlock when calling mul() from both() cause both() already acquire the mutex, so mul() should be blocked.

Now I am using: ubuntu 17.10.1 with g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0 (g++ --version output)

If I am using the compile command:

user@user: g++ -o out_deadlock main_deadlock.cpp

I get no deadlock at all!

But if I use the compile command:

user@user: g++ -std=c++11 -pthread -o out_deadlock main_deadlock.cpp

All works - means I see deadlock.

Can you explain? also, How the first command makes the code compile? I didn't "mention" pthreads and didn't mention -std=c++11 although code is using c++ 11 lib? I would expect fail of compilation/linkage?

Thanks.

Aucun commentaire:

Enregistrer un commentaire