Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::thread error (thread not member of std)

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?

like image 354
luis Avatar asked Mar 25 '10 21:03

luis


People also ask

What is std :: This_thread?

std::this_threadThis namespace groups a set of functions that access the current thread.

Is std :: thread copyable?

std::thread::operator= thread objects cannot be copied (2).

What does std :: thread join do?

std::thread::join. Blocks the current thread until the thread identified by *this finishes its execution. The completion of the thread identified by *this synchronizes with the corresponding successful return from join() .

Does MinGW support thread?

To use the MinGW-w64 with Win32 native threads you can install the mingw-std-threads headers. As described on that page, this is because MinGW-w64 is a port of GCC, but GCC does not include any native thread support.


1 Answers

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?

like image 131
Artem Sokolov Avatar answered Sep 18 '22 12:09

Artem Sokolov