c++ - std::mutex 的线程安全

标签 c++ multithreading thread-safety std mutex

我可以在一个线程中锁定 std::mutex 并在另一个线程中解锁吗?这是没有任何特定工程背景的理论问题。我只是好奇。

最佳答案

C++ 标准,[thread.mutex.requirements.mutex]/21 和 22:

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

      Requires: The calling thread shall own the mutex.

违反 Requires 子句中的要求会导致未定义的行为。

关于c++ - std::mutex 的线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27257440/

相关文章:

c++ - 在二叉树中插入 4 或 5 个数字,但在输出中只得到 3 个数字

C++内存对齐

c++ - 为什么在这种情况下 boost 不创建线程 (C2248) 以及如何创建它?

c++ - SFML在多线程中失败

vb.net - VB.NET 中的 Me.Invoke 实际上并不是 "Invoke"- 线程在 Invoke 语句上停滞

java - 锁定两种方法但允许一种方法运行多线程

c# - 使用线程 C#

c++ - 在 OpenMP 线程中充分私有(private)化数据

线程之间可以安全地共享整数吗?

multithreading - 线程安全地更新大数据矩阵: now using millions of mutexes?