C++ 如何使用 #include <thread> 编译代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8513980/
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-28 18:41:56  来源:igfitidea点击:

How to compile the code using #include <thread>

c++multithreading

提问by q0987

I am trying to compile some C++ code that uses threads:

我正在尝试编译一些使用线程的 C++ 代码:

#include <iostream>
#include <thread>
void hello()
{
    std::cout<<"Hello Concurrent World\n";
}

int _main(int argc, _TCHAR* argv[])
{
    std::thread t(hello);
    t.join();
    return 0;
}

I get errors when compiling:

编译时出现错误:

c:\temp\app1\app1\app1.cpp(6): fatal error C1083: Cannot open 
include file: 'thread': No such file or directory


~/Documents/C++ $ g++ -o thread1 thread1.cpp -D_REENTRANT -lpthread
In file included from /usr/include/c++/4.5/thread:35:0,
                 from thread1.cpp:2:
/usr/include/c++/4.5/bits/c++0x_warning.h:31:2: error: #error This file 
requires compiler and library support for the upcoming ISO C++ standard, 
C++0x. This support is currently experimental, and must be enabled with 
the -std=c++0x or -std=gnu++0x compiler options.

How do I fix these errors?

如何修复这些错误?

回答by Matteo Italia

<thread>and standard threading support is a new feature (defined in the C++11 standard). As for g++, you have to enable it adding -std=c++0xto the command line, as explained in the error message.

<thread>标准线程支持是一项新功能(在 C++11 标准中定义)。至于 g++,您必须启用它添加-std=c++0x到命令行,如错误消息中所述。

Also, you are using a nonstandard (Microsoft-specific) main, use the "classic" mainand normal char:

此外,您使用的是非标准(Microsoft 特定) main,使用“经典”main和正常char

// thread1.cpp
#include <iostream>
#include <thread>

void hello()
{
    std::cout<<"Hello Concurrent World\n";
}

int main(int argc, char * argv[])
{
    std::thread t(hello);
    t.join();

    return 0;
}

Notice that not all C++11 features are available in current compilers; as far as g++ is concerned, you can find the status of their implementation here.

请注意,并非所有 C++11 功能都在当前编译器中可用;就 g++ 而言,您可以在此处找到它们的实现状态。

回答by Basile Starynkevitch

Yes, the header file <thread>is only standard in the C++11 newest standard (finalized this year only).

是的,头文件<thread>只是 C++11 最新标准中的标准(仅在今年完成)。

With GCC, you'll need the very latest 4.6version, and even with it, not everything is supported of the C++11 standard. See this table

使用 GCC,您将需要最新的4.6版本,即使有了它,C++11 标准也并非支持所有内容。看这张表

回答by Michael Burr

As far as MSVC goes, the C++11 <thread>header isn't supported in VS2010 - you'll need to pull down a Developer Preview of Visual Studio 11 (http://msdn.microsoft.com/en-us/vstudio/hh127353) to try it out today.

就 MSVC 而言,<thread>VS2010 不支持C++11 标头 - 您需要下拉 Visual Studio 11 的开发人员预览版 (http://msdn.microsoft.com/en-us/vstudio/ hh127353)今天就试试。

See http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspxfor details on what is new in C++ for Visual Studio 11.

有关Visual Studio 11 的 C++ 新增功能的详细信息,请参阅http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

Also, my PDF of the book (which is in pre-release at the moment) has the following definition of main():

此外,我的这本书的 PDF(目前处于预发布阶段)具有以下定义main()

int main()
{
    std::thread t(hello);
    t.join();
}

which avoids the problems you're running into with _TCHARbeing undefined in GCC.

这避免了您_TCHAR在 GCC 中未定义时遇到的问题。