为什么这个简单的std::线程示例不起作用?

时间:2021-12-07 21:00:49

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。但是这两个都会导致示例中止。

$ ./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?

有人知道我错过了什么吗?

5 个解决方案

#1


45  

You have to join std::threads, just like you have to join pthreads.

您必须加入std:::threads,就像您必须加入pthreads一样。

int main( int argc, char *argv[] )
{
    std::thread t( doSomeWork );
    t.join();
    return 0;
}

UPDATE: This Debian bug report pointed 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:::线程代码稳定之前,这很可能是一种变通方法,并且g++在应该的时候(或者对于c++来说,总是这样)将该库拉进来。

#2


13  

Please use the pthread library during the compilation: g++ -lpthread.

在编译过程中请使用pthread库:g+ -lpthread。

#3


3  

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

#4


0  

For what it's worth, I had different issue with similar code using threads in 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(…)。

#5


-2  

You need to link to run time library

您需要链接到运行时库

#1


45  

You have to join std::threads, just like you have to join pthreads.

您必须加入std:::threads,就像您必须加入pthreads一样。

int main( int argc, char *argv[] )
{
    std::thread t( doSomeWork );
    t.join();
    return 0;
}

UPDATE: This Debian bug report pointed 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:::线程代码稳定之前,这很可能是一种变通方法,并且g++在应该的时候(或者对于c++来说,总是这样)将该库拉进来。

#2


13  

Please use the pthread library during the compilation: g++ -lpthread.

在编译过程中请使用pthread库:g+ -lpthread。

#3


3  

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

#4


0  

For what it's worth, I had different issue with similar code using threads in 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(…)。

#5


-2  

You need to link to run time library

您需要链接到运行时库