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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2519607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:46:34  来源:igfitidea点击:

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

c++multithreadinggccc++11std

提问by luis

I compiled & installed gcc4.4 using macports.

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

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

当我尝试使用 -> 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..

std::cout <<...编译正常..

Can anyone help me?

谁能帮我?

回答by Artem Sokolov

gcc does not fully support std::thread yet:

gcc 尚不完全支持 std::thread:

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

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

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

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

Use boost::threadin the meantime.

同时使用boost::thread

Edit

编辑

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

尽管以下内容在 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:

的输出a.out

Printing from another thread

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

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

回答by Tronic

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.

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

回答by Vlad Bezden

I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threadsIncluding 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

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

#include "mingw.thread.h"

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