我正在编写一个简单的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
。为什么会出现这个错误? 难道编译器不支持锁的使用吗?
-static
。唯一的问题是我只能找到一个与GCC 4.7.0配套的,这意味着要放弃其他一些C++11特性,直到构建出新的版本。 - chris