std::mutex 如何在不同的线程中解锁?

How std::mutex got unlocked in different thread?

我正在阅读二进制信号量和互斥量 (Difference between binary semaphore and mutex) 之间的差异,我想验证的一件事是,当任务锁定(获取)互斥量时,它只能解锁(释放)它。如果另一个任务试图解锁它尚未锁定(因此不拥有)的互斥锁,则会遇到错误情况,最重要的是,互斥锁未解锁,为此我在 c++14 中创建了以下代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

int counter;
int i;
std::mutex g_pages_mutex;
void increment()
{
    std::cout<<"increment...."<<std::endl;    
    g_pages_mutex.lock();
    bool flag = g_pages_mutex.try_lock();
    std::cout<<"increment Return value is "<<flag<<std::endl;
    counter++;
    std::this_thread::sleep_for(5s);
}
void increment1()
{
    std::this_thread::sleep_for(5s);    
    std::cout<<"increment1...."<<std::endl;       
    g_pages_mutex.unlock();    
    counter++;
    bool flag = g_pages_mutex.try_lock();
    std::cout<<"increment1 Return value is "<<flag<<std::endl;
}
int main()
{
    counter = 0;
    std::thread t(increment);
    std::thread t1(increment1);
    t.join();
    t1.join();
    return 0;
}

然而,通过这个例子,我能够从不拥有互斥锁的线程中解锁互斥锁,所以只是想知道是否存在一些理解上的差距,或者这个问题在 c++14 std::mutex 中吗?

来自 cppreference std::mutex(强调我的):

The behavior of a program is undefined if a mutex is destroyed while still owned by any threads, or a thread terminates while owning a mutex.

来自 std::mutex::try_lock and as 上的同一站点:

If try_lock is called by a thread that already owns the mutex, the behavior is undefined.

还有std::mutex::unlock and as :

The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.

您的函数和线程都会导致未定义的行为:

  1. increment 调用 lock() 然后 try_lock():未定义的行为
  2. increment1 在拥有互斥量之前调用 unlock():未定义的行为

如果您在线程结束前不调用 unlock(),从 increment 中删除 try_lock() 仍然会导致未定义的行为。

您应该更喜欢使用 std::lock_guard, or for a simple int you could also use std::atomic

在调用线程拥有的 std::mutex(非递归)上调用 try_lock,在调用线程不拥有的互斥量上调用 unlock,并结束线程持有互斥锁时,所有这些都会导致未定义的行为。

它可能看起来成功,也可能失败并抛出异常,它可能格式化你的硬盘,它可能召唤鼻恶魔,它可能时间旅行并为你更正你的代码,或者它可能做其他事情。就标准而言,任何都是允许的。

根据 (std)30.4.1.2:

,调用 unlock 的先决条件是持有互斥锁的所有权

The expression m.unlock() shall be well-formed and have the following semantics:

Requires: The calling thread shall own the mutex.

由于线程执行 increment1 不持有互斥锁的所有权,因此它引发了未定义的行为。