32

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

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

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

编译器返回:

 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

但是std::cout <<...编译很好..

谁能帮我?

4

3 回答 3

15

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;
}

编译

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

输出a.out

从另一个线程打印

你能提供完整的代码吗?也许那些s中潜伏着一些晦涩的问题...

于 2010-03-26T03:24:12.467 回答
6

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

于 2010-03-25T21:50:46.087 回答
6

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

#include "mingw.thread.h"

...
std::thread t(handle);
...
于 2016-11-30T19:49:06.973 回答