35

I compiled & installed gcc4.4 using macports.

When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:

 #include <thread>
 ...
  std::thread t(handle);
  t.join();
 ....

The compiler returns:

 cserver.cpp: In member function 'int CServer::run()':
 cserver.cpp:48: error: 'thread' is not a member of 'std'
 cserver.cpp:48: error: expected ';' before 't'
 cserver.cpp:49: error: 't' was not declared in this scope

But std::cout <<... compiles fine..

Can anyone help me?

3
  • If you look in the thread header, it appears that the class only exists #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1). I'm not sure though, what you'd have to do to have those defined. Commented Mar 25, 2010 at 22:46
  • @UncleBens: I believe those are directly defined by -pthread and -std=c++0x. Omitting -pthread causes a seg fault: gcc.gnu.org/ml/gcc-help/2009-04/msg00208.html Commented Mar 26, 2010 at 3:47
  • Just the latest update: MacPorts gcc 4.7.0 supports/compiles std::thread, while 4.6.3 does not. Commented Apr 26, 2012 at 9:45

4 Answers 4

15

gcc does not fully support std::thread yet:

http://gcc.gnu.org/projects/cxx0x.html

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

Use boost::thread in the meantime.

Edit

Although the following compiled and ran fine for me with gcc 4.4.3:

#include <thread>
#include <iostream>

struct F
{
  void operator() () const
  {
    std::cout<<"Printing from another thread"<<std::endl;
  }
};

int main()
{
  F f;
  std::thread t(f);
  t.join();

  return 0;
}

Compiled with

g++ -Wall -g -std=c++0x -pthread main.cpp

Output of a.out:

Printing from another thread

Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your code and I get the same error... Can this be OSX related? Or maybe something that isn't right with the GCC install by MacPorts?
It may be that MacPorts doesn't fully support c++0x functionality? Do you have the output from your gcc configure script? Here's one Mac user for whom configure specified that std::thread is not supported: (mail-archive.com/[email protected]/msg00973.html)
7

I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threads Including mingw.mutex.h, mingw.thread.h files to global MinGW directory fixed this issue. All I had to do is to include header file and my code stayed the same

#include "mingw.thread.h"

...
std::thread t(handle);
...

2 Comments

Cheers for this. Was driving me crazy. Your answer was slightly confusing where you referred to the global MinGW directory. I had to drop the files in the project folder where all the other .h files are to get it work... but it worked.
Well what do you mean global directory, I am trying to find where to insert it tried globally same level with /bin /include doesn't work. thank you in advance
6

Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.

3 Comments

The macro name actually has two underscores at the end, but SO misinterprets it as formatting if I write it correctly.
Well pointed out, I removed it, but it still gives me the same error.. I tried with gcc4.4 and gcc4.5 beta... This is frustrating.
fixed the missing underscore problem.
0

Update your MinGW compiler to the latest version by using MSYS2 for this error or try reinstalling MinGW by using MSYS2

cserver.cpp:48: error: 'thread' is not a member of 'std'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.