C++ 为什么这个简单的 std::thread 示例不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6485705/
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
Why does this simple std::thread example not work?
提问by Stéphane
Tried the following example compiled with g++ -std=gnu++0x t1.cpp
and g++ -std=c++0x t1.cpp
but both of these result in the example aborting.
尝试使用g++ -std=gnu++0x t1.cpp
和编译以下示例,g++ -std=c++0x t1.cpp
但两者都导致示例中止。
$ ./a.out
terminate called after throwing an instance of 'std::system_error'
what():
Aborted
Here is the sample:
这是示例:
#include <thread>
#include <iostream>
void doSomeWork( void )
{
std::cout << "hello from thread..." << std::endl;
return;
}
int main( int argc, char *argv[] )
{
std::thread t( doSomeWork );
t.join();
return 0;
}
I'm trying this on Ubuntu 11.04:
我正在 Ubuntu 11.04 上尝试这个:
$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Anyone knows what I've missed?
有谁知道我错过了什么?
回答by rubenvb
You have to join
std::thread
s, just like you have to join pthreads
.
你必须join
std::thread
s,就像你必须加入一样pthreads
。
int main( int argc, char *argv[] )
{
std::thread t( doSomeWork );
t.join();
return 0;
}
UPDATE:This Debian bug reportpointed me to the solution: add -pthread
to your commandline. This is most probably a workaround until the std::thread
code stabilizes and g++ pulls that library in when it should (or always, for C++).
更新:此 Debian 错误报告向我指出了解决方案:添加-pthread
到您的命令行。这很可能是一种解决方法,直到std::thread
代码稳定下来并且 g++ 在它应该(或总是,对于 C++)拉入该库之前。
回答by SarB
Please use the pthread library during the compilation: g++ -lpthread.
编译时请使用pthread库:g++ -lpthread。
回答by Eric Leschinski
Simplest code to reproduce that error and how to fix:
重现该错误的最简单代码以及如何修复:
Put this in a file called s.cpp:
把它放在一个名为 s.cpp 的文件中:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <thread>
using namespace std;
void task1(std::string msg){
cout << "task1 says: " << msg;
}
int main(){
std::thread t1(task1, "hello");
usleep(1000000);
t1.detach();
}
Compile like this:
像这样编译:
el@apollo:~/foo7$ g++ -o s s.cpp -std=c++0x
Run it like this, the error happens:
像这样运行它,错误发生:
el@apollo:~/foo7$ ./s
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
To fix it, compile it like this with the -pthread flag:
要修复它,请使用 -pthread 标志像这样编译它:
g++ -o s s.cpp -std=c++0x -pthread
./s
Then it works correctly:
然后它正常工作:
task1 says: hello
回答by Petr Vep?ek
For what it's worth, I had different issuewith similar codeusing threadsin g++ (MinGW). Work-around was to put some "delay" between creating a thread and joining it.
对于它的价值,我有不同的问题有类似的代码使用线程在G ++(MinGW的)。解决方法是在创建线程和加入线程之间放置一些“延迟”。
Code with infrequently failing assertion:
断言很少失败的代码:
std::atomic_bool flag{false};
std::thread worker( [&] () { flag.store(true); } );
worker.join();
assert(flag.load()); // Sometimes fails
Work-around:
解决办法:
std::atomic_bool flag{false};
std::thread worker( [&] () { flag.store(true); } );
while (not flag.load()) { std::this_thread::yield(); }
worker.join();
assert(flag.load()); // Works fine
Note that yield()
alone did not help, hence the while loop. Using sleep_for(...)
also works.
请注意,yield()
单独没有帮助,因此是 while 循环。使用sleep_for(...)
也有效。
回答by CPlusPlusUser
You need to link to run time library
您需要链接到运行时库