C++ mutex in namespace std does not name a type

2019-01-11 22:39发布

I'm writing a simple C++ program to demonstrate the use of locks. I am using codeblocks and gnu gcc compiler.

 #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;
}

I'm getting the following error: mutex in namespace std does not name a type.

Why am I getting this error? Doesn't the compiler support use of locks?

9条回答
啃猪蹄的小仙女
2楼-- · 2019-01-11 23:29

I don't know if it works for everybody, but in other way you just have to update your ndk. I'm using ndk-r11c and it works perfectly.

查看更多
爷、活的狠高调
3楼-- · 2019-01-11 23:30

Many classes of the standard thread library can be replaced with the boost ones. A very easy workaround is to change the entire standard mutex file with a couple of lines.

#include <boost/thread.hpp>

namespace std
{
   using boost::mutex;
   using boost::recursive_mutex;
   using boost::lock_guard;
   using boost::condition_variable;
   using boost::unique_lock;
   using boost::thread;
}

And do not forget to link against boost thread library.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-11 23:33

I got the same error with gcc4.7.7.

After adding "-std=c++0x", it is fixed.

查看更多
登录 后发表回答