pthread_self on Linux
pthread_self on Linux
我有以下小型代码段:
int main()
{
pthread_t t = pthread_self();
std::cout << t << std::endl;
return 0;
}
当我使用G 4.9.2在Linux上编译 链接时,输出为:
0
当我与pthread链接如下:
g++ test.c -o test -lpthread; ./test
输出为:
139675118393152
与-lpthreads链接时,我可以从实际的posix线程ID中获取映射到线程独有的索引吗?我想拥有一个具有某些特定于线程的值的全局数组,需要使用线程ID作为数组索引,无法处理139675118393152,需要将其映射到1、2等。p>
或多或少是如下:首先,由于在标准C库中实现了pthread_self()
,因此不需要链接到-lpthreads
。
现在,pthread_self()
使用全局变量,一个指向TCB(线程控制块(的指针,以存储线程信息,包括ID(过程中唯一(。
此指针初始化为null(0(,但是pthreads库(链接时(更改它,因此它现在指向当前的线程标头结构。
这就是为什么当您与pthreads链接时不链接到0时获得0的原因。
自定义线程IDS
您可以在创建时为每个线程分配一个自定义ID,并将该值用作数组的索引。
void* thread_function(void* data) {
assert(data);
const int id = *((int*)data);
// g_array[id]...
}
int main() {
// ...
pthread_t t0;
int t0id = 0; // this variable must exist when the thread starts
pthread_create(&t0, NULL, thread_function, &t0id);
pthread_t t1;
int t1id = 1; // this variable must exist when the thread starts
pthread_create(&t1, NULL, thread_function, &t1id);
// ...
pthread_join(t0, NULL);
pthread_join(t1, NULL);
}
另一个选项可能是使用全局std::map<pthread_t, int> g_thread_ids
结构并链接来自pthread_self()
的线程ID,并以参数传递的数组索引。您必须谨慎对待种族条件(为简单起见,此处省略了(。您还应该关心不会以这种方式创建线程的情况(如果可能的话(,因为pthread_self()
值在地图中不存在。
std::map<pthread_t, int> g_thread_ids;
int get_thread_index() { // note: critical section
if (g_thread_ids.find(pthread_self()) == g_thread_ids.end()) return -1;
return g_thread_ids[pthread_self()];
}
void* thread_function(void* data) {
assert(data);
const int id = *((int*)data); // read the index from caller
g_thread_ids[pthread_self()] = id; // note: critical section
// g_array[get_thread_index()]...
}
相关文章:
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- SFML 交叉编译 for Windows on Linux.
- OpenAL C++ on Linux
- Valgrind on codeblocks (linux)
- CMake FindJNI issue on linux
- pthread_self on Linux
- std::vector sort() on Linux?
- Building OpenCV 2.4.11 on Linux with `libopencv_ffmpeg.so`
- 在Linux上使用QT,有没有办法禁用"Always on Top"?
- 在 Windows for Linux on Intel 上编译C++库
- 计算"copying a binary file in c++ on linux"消耗时间的最佳方法是什么?
- C++ [[gnu::visibility( "default" )]] vs __declspec(dllexport) on Windows 和 Linux
- XRE_InitEmbedding2 SIGSEGV on Linux with XULRunner 15
- boost::mutex::timed_lock not on Linux
- Netbeans C/C++ on Linux "step in to" Linux C 运行时代码?
- gdb fork() exec on Linux
- flock-ing a C++ ifstream on Linux (GCC 4.6)
- C++ GetModuleBase on linux
- _vscwprintf on Mac OS X/Linux
- RTLD_LOCAL and dynamic_cast on Linux