嗨,我正在尝试使用std::threadG ++.这是我的测试代码
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
Run Code Online (Sandbox Code Playgroud)
它编译,但当我尝试运行它时,结果是:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
Run Code Online (Sandbox Code Playgroud)
我的编译器版本:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
我的测试代码出了什么问题?
更新:我使用以下命令行来编译和运行我的代码.
$ g++ -std=c++0x test.cpp
$ ./a.out
Run Code Online (Sandbox Code Playgroud)
我试过 …
我有最简单的代码:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "another thread";
}
int main()
{
std::thread t(worker);
std::cout << "main thread" << std::endl;
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然我仍然无法编译它g++来运行.
更多细节:
$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
编译命令:
$ g++ main.cpp -o main.out -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
运行:
$ …Run Code Online (Sandbox Code Playgroud) 有没有办法检查哪些库是正在运行的进程?
更具体地说,如果程序使用dlopen加载某些共享库,则readelf或ldd不会显示它.是否有可能从正在运行的进程中获取该信息?如果有,怎么样?
在Linux中,我有一个使用pthreads的共享库,而没有使用的是main.c.
libpthread.so显示在我的共享库的ldd中,这是正确的.
$ ldd libmapreduce.so.1.0
linux-gate.so.1 => (0x0067d000)
libpthread.so.0 => /lib/libpthread.so.0 (0x0058c000)
[...]
Run Code Online (Sandbox Code Playgroud)
但是当我编译并链接我的main.c,它不使用pthreads到我的共享库时,我看到:
$ icc -Wall -o main main.c -lmapreduce
/opt/intel/Compiler/11.1/046/lib/ia32/libiomp5.so: undefined reference to `pthread_atfork'
Run Code Online (Sandbox Code Playgroud)
将-lpthread添加到我的编译命令,即
$ icc -Wall -o main main.c -lmapreduce -lpthread
Run Code Online (Sandbox Code Playgroud)
解析未定义的引用.
当我的main.c不使用它并且我的共享库已经链接了libpthread时,为什么我需要显式链接到libpthread?