我试图在Eclipse中测试一个C ++ 11线程的例子。但是在运行程序时我收到了这条消息:
在抛出'std :: system_error'的实例后终止调用
what():不允许操作'
我的系统:ubuntu + gcc 4.7
程序:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "hello from worker" << std::endl;
}
int main(int argc, char **argv)
{
std::thread t(worker);
t.join();
}
......是的,我说 -std=c++11
和 -pthread
内 C/C++ Build -> Settings -> Tool Settings -> Cross G++ Compiler -> Miscellaneous -> Other Flags
。
任何意见?
Jonathan Wakely的评论解决了这个问题。
我补充道 -pthread
至 C/C++ Build -> Settings -> Tool Settings -> Cross G++ **Linker** -> Miscellaneous -> Other Flags
并且该程序正常工作。
谢谢乔纳森。
工作C ++ 11 std::thread
在Eclipse中,需要给予 -pthread
编译时的选项。然而 这还不够。在我的Ubuntu 14.04中,使用Eclipse Kepler和下面的g ++ 4.9使其工作:
- 右键单击Project并选择“Properties”
- 转到“C / C ++ Build”>“设置”>(选项卡)“工具设置”
- 首先选择'Cross G ++ Compiler'>'Miscellaneous'>'Other flags';
并添加
-pthread
后 -std=c++11
- 第二步选择'Cross G ++ Linker'>'Libraries';
并添加 pthread
(相当于
命令行 -lpthread
)
最后重新编译项目;错误应该去。
还要记住,如果你使用, std::thread
那么它的对象必须是 join()
某处。否则,您可能会遇到运行时错误:
终止被调用而没有活动异常