linux 下獲取執行緒ID

2021-07-01 23:53:11 字數 895 閱讀 8936

linux多執行緒環境下gettid() pthread_self() 兩個函式都獲得執行緒id,但這2個id有所不同

gettid是核心中的執行緒的id:

posix thread id可以在乙個程序內唯一標識乙個執行緒,但如果放到系統範圍內的話就得用gettid了。

#include // 需要包含這個標頭檔案
inline pid_t 	my_gettid()		// gettid是核心中的執行緒的id

pthread_self是posix thread id:

建立執行緒函式返回的第乙個引數就是 

pthread_self()

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg); 

同時 pthread_detach,pthread_cancel等函式是需要posix thread id為引數的

總結:執行緒庫實際上由兩部分組成:核心的執行緒支援+使用者態的庫支援(glibc),linux在早期核心不支援執行緒的時候glibc就在庫中(使用者態)以纖程(就是使用者態執行緒)的方式支援多執行緒了,posix thread只要求了使用者程式設計的呼叫介面對核心介面沒有要求。linux上的執行緒實現就是在核心支援的基礎上以posix thread的方式對外封裝了介面,所以才會有兩個id的問題。

Linux下獲取執行緒ID的方法

linux下多執行緒程式發生coredump時,用 gdb path to program file core 可以看到所有執行緒 root rubic test thread gdb a.out core gnu gdb gdb 7.6.1 license gplv3 gnu gpl versio...

linux下的執行緒ID和程序ID

在描述執行緒id和程序id之前我們先來分清楚幾個概念 1.使用者級執行緒和核心級執行緒 什麼是使用者級執行緒?使用者級執行緒核心的切換由使用者態程式自己控制核心切換,不需要核心干涉,少了進出核心態的消耗,但不能很好的利用多核cpu,目前linux pthread大體是這麼做的。執行緒組id?執行緒組...

c 獲取執行緒id 高效獲取當前執行緒的 id

可以利用微軟提供的 api,輕鬆獲取當前執行緒的 id int id getcurrentthreadid linux 平台一般用的是 pthread 執行緒庫,但它並不提供獲取執行緒內部 id 的方法。可以通過系統呼叫得到當前執行緒的 id include for syscall include ...