c++ double shared_lock freezes the program on windows

303 Views Asked by At

When I am learning shared_mutex in C++17, I find a weired problem. If I call shared_lock twice in one thread, and call unique_lock in another thread, then my program will freeze. Like this:

std::mutex mutex;
std::shared_mutex s_mutex;

int i = 0;

void func() {
    auto lock = std::shared_lock(s_mutex);
    auto lock1 = std::shared_lock(s_mutex);
    ++i;
}

void func2() {
    auto lock = std::unique_lock(s_mutex);
    ++i;
}

int main() {
    auto t1 = std::thread([](){
        auto i = 10000;
        while(i--) {
            func();
        }
    });
    auto t2 = std::thread([](){
        auto i = 10000;
        while(i--) {
            func2();
        }
    });
    t2.join();
    t1.join();
    std::cout << i << std::endl;
    return 0;
}

This problem seems only appear on windows, I tried it on Arch linux, it works well.

I'm using g++.exe (Rev2, Built by MSYS2 project) 12.1.0.

I tried add cout after every lock like:

void func() {
    auto lock = std::shared_lock(s_mutex);
    std::cout << "t1 lock1\n";
    auto lock1 = std::shared_lock(s_mutex);
    std::cout << "t1 lock2\n";
    ++i;
}

Then I find it always freezes after t1 lock1\n, so I tried to step into lock1's constructor, and I find it freeze after this function:

static inline int
__gthread_active_p (void)
{
  return 1;
}

But I don't know why. Thanks in advance.

1

There are 1 best solutions below

2
user17732522 On

Trying to lock the mutex again in a thread that already holds a lock on the mutex has undefined behavior.

This applies to all mutex types except the recursive* ones.