https://coderwall.com/p/rzkw6q
Consider the following code, which uses the C++11 (used to be known as C++0x) thread library:
#include <iostream>
#include <thread>
using namespace std;
void hello_world()
{
cout << "Hello from thread!\n";
}
int main()
{
thread t(hello_world);
t.join();
return 0;
}
How can we get this to work? If we compile it with:
We'll get something like:
... and maybe something about the type used in the code:
So we add the ...
No errors during compilation, but when we try to execute ...
... oops. We have to link this against the operating system's implementation of threads. In linux, this is the library. The winning command is:
and then:
本文详细介绍了如何在C++11中正确使用线程库。首先,需要确保编译器支持C++11标准,并通过指定编译选项来启用。其次,为使程序能够成功运行,还需链接到操作系统提供的线程库,如Linux下的libpthread。文章通过一个简单的示例程序,展示了从编译到执行过程中可能遇到的问题及解决方案。
9306

被折叠的 条评论
为什么被折叠?



