在 Linux 中构建 C++11 个可线程翻译单元
Building C++11 threadable translation-units in linux
给定一个使用 C++11 线程功能的简单程序:
#include <iostream>
#include <thread>
using namespace std;
void dont_thread_on_me() {
cout << "action from another thread" << endl;
}
int main() {
thread t { dont_thread_on_me };
t.detach();
cin.get();
}
如果我使用以下方法构建应用程序:
c++ -std=c++11 Program.cpp -o Program.out
该程序构建良好,但是当我运行它时,我得到:
./Program.out
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
如果我使用-pthread
构建它,如下所示:
c++ -std=c++11 Program.cpp -o Program.out -pthread
程序执行正常。我还没有看到任何其他需要特殊构建标志的 C++11 功能,为什么这个?
Operation not permitted
异常是在 gcc-4.7.3/libstdc++-v3/src/c++11/thread.cc
中定义的名为 _M_start_thread
的函数中引发的。
如果名为 __gthread_active_p
的函数返回 null 值,则会引发此异常。此函数是 gcc-4.7.3/libgcc/gthr-posix.h
中定义的C
函数。
此方法在 pthread 函数的libc
中四处闲逛,并且可能执行一些时髦的运行时/懒惰绑定。您的C++代码链接没有问题,因为它不包含对 pthread 的任何链接时间依赖。C 代码可以容忍它将在运行时尝试解析的函数缺少链接值。
不幸的是,我们通常用来捕获这些问题的C++健全性检查被绕过了,因为 pthread 链接是通过 C 代码中的惰性绑定完成的。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 在 Linux 中构建 C++11 个可线程翻译单元