std :: thread错误(线程不是std的成员)

lui*_*uis 23 c++ multithreading gcc std c++11

我使用macports编译并安装了gcc4.4.

当我尝试使用 - > g ++ -g -Wall -ansi -pthread -std = c ++ 0x main.cpp ...进行编译时:

 #include <thread>
 ...
  std::thread t(handle);
  t.join();
 ....
Run Code Online (Sandbox Code Playgroud)

编译器返回:

 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
Run Code Online (Sandbox Code Playgroud)

std::cout <<...编译好..

谁能帮我?

Art*_*lov 14

gcc还没有完全支持std :: thread:

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

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

在此期间使用boost :: thread.

编辑

虽然以下编译并运行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;
}
Run Code Online (Sandbox Code Playgroud)

编译

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

产量a.out:

Printing from another thread

你能提供完整的代码吗?也许潜伏在那些...s中有一些模糊不清的问题?


Vla*_*den 6

我在使用 MinGW 的 Windows 上遇到了同样的问题。我在 github mingw-std-threads上找到了 in 的包装类,包括 mingw.mutex.h、mingw.thread.h 文件到全局 MinGW 目录修复了这个问题。我所要做的就是包含头文件并且我的代码保持不变

#include "mingw.thread.h"

...
std::thread t(handle);
...
Run Code Online (Sandbox Code Playgroud)


Tro*_*nic 5

删除-ansi,它意味着-std = c ++ 98,你显然不想要它.它还会导致__STRICT_ANSI__定义宏,这可能会改变标头的行为,例如通过禁用C++ 0x支持.