34

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?

Xeo
  • 129,499
  • 52
  • 291
  • 397
luis
  • 345
  • 1
  • 3
  • 4
  • 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. – UncleBens Mar 25 '10 at 22:46
  • @UncleBens: I believe those are directly defined by -pthread and -std=c++0x. Omitting -pthread causes a seg fault: http://gcc.gnu.org/ml/gcc-help/2009-04/msg00208.html – Artem Sokolov Mar 26 '10 at 03:47
  • Just the latest update: MacPorts gcc 4.7.0 supports/compiles std::thread, while 4.6.3 does not. – P Marecki Apr 26 '12 at 09:45

3 Answers3

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?

Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
  • 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? – luis Mar 26 '10 at 09:54
  • 1
    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: (http://www.mail-archive.com/dealii@dealii.org/msg00973.html) – Artem Sokolov Mar 26 '10 at 16:44
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);
...
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • 1
    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. – CoreyRS Jan 13 '18 at 10:20
  • 1
    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 – Gerasimos Ragavanis Feb 18 '22 at 10:43
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.

deft_code
  • 57,255
  • 29
  • 141
  • 224
Tronic
  • 10,250
  • 2
  • 41
  • 53
  • The macro name actually has two underscores at the end, but SO misinterprets it as formatting if I write it correctly. – Tronic Mar 25 '10 at 21:54
  • 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. – luis Mar 25 '10 at 21:55
  • fixed the missing underscore problem. – deft_code Mar 26 '10 at 14:23