C++ C栋和x2B栋+;linux中的11个线程转换单元
给出一个使用C++11线程功能的简单程序:C++ C栋和x2B栋+;linux中的11个线程转换单元,c++,linux,c++11,C++,Linux,C++11,给出一个使用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()
#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();
}
程序构建得很好,但当我运行它时,我得到:
./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功能,为什么会出现这种情况?在
gcc-4.7.3/libstdc++-v3/src/C++11/thread.cc
中定义的名为\u start\u thread
的函数中抛出了异常
如果名为\ugthread\u active\u p
的函数返回空值,则会引发此异常。此函数是在gcc-4.7.3/libgcc/gthr posix.h
中定义的C
函数
该方法在libc
中搜索pthread函数,并可能执行一些时髦的运行时/惰性绑定。您的C++代码链接没有问题,因为它不包含任何对p螺纹的链接时间依赖性。C代码可以容忍在运行时尝试解析的函数缺少链接值
遗憾的是,C++正常性检查通常依赖于捕获这些问题,因为p螺纹连接是通过C代码中的惰性绑定完成的。
c++ -std=c++11 Program.cpp -o Program.out -pthread