为什么在Visual Studio 2013上的std::this_thread::sleep_for上死锁
Why deadlock on std::this_thread::sleep_for on Visual Studio 2013
下面的代码永远无法在VS 2013上打印m4
(当然也没有t2
和t3
(。它表现得像一个死锁,我不知道原因。我错过了什么吗?
#include <mutex>
#include <thread>
#include <chrono>
#include <stdio.h>
std::mutex m;
void work()
{
printf("t1n");
m.lock();
printf("t2n");
m.unlock();
printf("t3n");
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("m1n");
m.lock();
printf("m2n");
std::thread *th = new std::thread(work);
printf("m3n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
printf("m4n");
m.unlock();
printf("m5n");
th->join();
return 0;
}
编辑:是的,我看不到死锁。但只需在VS 2013中尝试一下。它锁定。我想知道原因。这是 VS 错误还是出了问题?
程序使用以下输出运行:
m1
m2
t1
m3
它永远阻止std::this_thread::sleep_for(std::chrono::milliseconds(1000));
,但为什么呢?
我认为这是VS 2013更新5中的一个错误。在 Ubuntu 16.04.5 中使用 GCC 6.0 无法重现此问题。此外,这可能是与std::this_thread::sleep_for
功能相关的错误,该错误会导致内部死锁与std::mutex
。因此,请谨慎使用它。
相关文章:
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 在std::thread中,joinable()然后join()线程安全吗
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 分离一个静态常量 std::thread?
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 将 std::thread by 值推送到列表中
- 转发变量参数列表以模拟 std::thread
- 嵌入式设备 -> std::thread -> FreeRTOS?
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何防止 std::thread 在 QT 中冻结 GUI?
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- C++ - Thread Pool
- 为什么参数在构造 std::thread 时移动两次
- std::thread::_Invoker 使用线程编程时出错
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接