带有代码:的线程在运行时阻止错误
thread with code::blocks error at runtime
编译此文件:
//Create a C++11 thread from the main program
#include <iostream>
#include <thread>
//This function will be called from a thread
void call_from_thread()
{
std::cout << "Hello, from thread! " << std::endl;
}
int main()
{
std::cout << "Hello, from main! " << std::endl;
//Launch a thread
std::thread t1(call_from_thread);
//Join the thread with the main thread
t1.join();
return 0;
}
使用选中copmpiler选项"use C++11"的代码::块。
代码::块编译器说:g++-Wall-fexceptions-g-std=c++11-pthread-c/home/main.cpp-o obj/Debug/main.o
请注意,-std=c++11-pthread已传递给编译器。
运行程序时,收到以下消息:
Hello, from main!
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
Process returned 134 (0x86) execution time : 0.110 s
Press ENTER to continue.
如何完成这项工作?谢谢你的帮助。
====================
PS。我看过这篇用g++编译多线程代码的文章。然后尝试使用技巧
我试过这个:
g++-Wall-fexceptions-g-std=c++11-pthread-Wl-无需-c/home/olivier/main.cpp-o obj/Debug/main.o
但得到了以下内容。
g++: error: unrecognized command line option ‘-Wl’
g++: error: unrecognized command line option ‘--no-as-needed’
如果使用-phread编译,则应使用-lpthread链接。
第二个很简单,它必须是:-Wl,--no-as-needed
(缺少","),但它用于链接器。你的调用只编译成一个对象文件,所以你可以擦除它。
关于第二个注意事项,可能是由于某些gcc版本中的错误,您实际上必须与-Wl,--no-as-needed
链接
对于代码::块,您可以在构建选项的链接器选项卡中添加-Wl,--no-as-needed
和-lpthread。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 试图找出为什么我会收到运行时错误?
- 迭代二维矢量时发生运行时错误
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误