Reputation: 2421
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?
Upvotes: 44
Views: 113886
Reputation: 7519
I encountered this same problem when using MingW-W64 7.2.0. I tested out several different Windows builds from the mingw-64 download page, and found that MinGW-W64 GCC-8.1.0 supports mutex
and contains the pthread
library. When installing, I selected the following options:
My multi-threaded code based on pthreads
now compiles and runs cleanly on both Windows and Linux with no changes.
This version is leaner than the 7.3.0 build I was using because it doesn't have a CygWin environment or package manager. I also copied mingw32-make.exe
to make.exe
so my Makefile wouldn't need to be modified. The installer creates a "Run terminal" link in the Windows Start Menu.
Upvotes: 5
Reputation: 11
I fixed it by following steps:
Upvotes: 1
Reputation: 65
my gcc version is 5.4 and i solved this problem when adding #include and also adding -std=c++11 at my CmakeLists.txt as below:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native -std=c++11")
Upvotes: 1
Reputation: 5582
I happened to be looking at the same problem. GCC works fine with std::mutex
under Linux. However, on Windows things seem to be worse. In the <mutex> header file shipped with MinGW GCC 4.7.2 (I believe you are using a MinGW GCC version too), I have found that the mutex class is defined under the following #if
guard:
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
Regretfully, _GLIBCXX_HAS_GTHREADS
is not defined on Windows. The runtime support is simply not there.
You may also want to ask questions directly on the MinGW mailing list, in case some GCC gurus may help you out.
EDIT: The MinGW-w64 projects provides the necessary runtime support. Check out http://mingw-w64.sourceforge.net/ and https://sourceforge.net/projects/mingw-w64/files/. Also, as 0xC0000022L pointed out, you need to download the POSIX thread version (I missed mentioning it last time).
Upvotes: 24
Reputation: 13729
Use POSIX threading model for MINGW:
$ sudo update-alternatives --config i686-w64-mingw32-gcc
<choose i686-w64-mingw32-gcc-posix from the list>
$ sudo update-alternatives --config i686-w64-mingw32-g++
<choose i686-w64-mingw32-g++-posix from the list>
$ sudo update-alternatives --config x86_64-w64-mingw32-gcc
<choose x86_64-w64-mingw32-gcc-posix from the list>
$ sudo update-alternatives --config x86_64-w64-mingw32-g++
<choose x86_64-w64-mingw32-g++-posix from the list>
See also: mingw-w64 threads: posix vs win32
Upvotes: 26
Reputation: 135
I got the same error with gcc4.7.7.
After adding "-std=c++0x", it is fixed.
Upvotes: 2
Reputation: 393
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.
Upvotes: 0
Reputation: 1533
This has now been included in MingW (Version 2013072300). To include it you have to select the pthreads package in the MinGW Installation Manager.
Upvotes: 13
Reputation: 147
Mutex, at least, is not supported in 'Thread model: win32' of the Mingw-builds toolchains. You must select any of the toolchains with 'Thread model: posix'. After trying with several versions and revisions (both architectures i686 and x86_64) I only found support in x86_64-4.9.2-posix-seh-rt_v3-rev1 being the thread model, IMO, the determining factor.
Upvotes: 6
Reputation: 6240
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.
Upvotes: -9