我正在编写一个简单的 C++ 程序来演示锁的使用。我正在使用 codeblocks
和 gnu
gcc
编译器。
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int x = 0; // shared variable
void synchronized_procedure()
{
static std::mutex m;
m.lock();
x = x + 1;
if (x < 5)
{
cout<<"hello";
}
m.unlock();
}
int main()
{
synchronized_procedure();
x=x+2;
cout<<"x is"<<x;
}
我收到以下错误: mutex in namespace std does not name a type
。
为什么我会收到此错误?编译器不支持使用锁吗?
原文由 arjun 发布,翻译遵循 CC BY-SA 4.0 许可协议
我安装了 Cygwin 而不是 WinGW。 youtube 上的说明 https://www.youtube.com/watch?v=DAlS4hF_PbY&t=245s