日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ceph bluestore源码分析:C++ 获取线程id

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ceph bluestore源码分析:C++ 获取线程id 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

閱讀ceph源碼過程中需要明確當前操作是由哪個線程發出,此時需要根據線程id來確認線程名稱
C++獲取線程id是通過系統調用來直接獲取

函數描述

頭文件:<sys/syscall.h>
函數名稱:syscall(SYS_gettid)
該函數直接返回了一個pid_t int類型的數字,即為當前線程id

此外函數pthread_self同樣能夠獲取線程id,但是該函數獲取到的線程id為posix的線程id,并不是真實的線程id.所以最后獲取到的數值是一個特別長的標識,并非top -Hp pid中的實際線程id

函數使用

編寫如下代碼:

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <pthread.h>
#define gettid() syscall(SYS_gettid)
using namespace std;void *threadFunc(void *p)
{printf("go into threadfunc ,pthread_self id is %d\n",pthread_self());printf("go into  threadfunc ,gettid id is %d \n",gettid());int i = 0;while(1){i++;sleep(1);}return NULL;
}int main()
{printf("man thread  tid is %d \n man pid is %d\n",gettid(),getpid());pthread_t id;pid_t tid;pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);int i=0;while (1){i++;sleep(1);}return 0;
}

運行結果如下:

[root@node1 ~]# ./a.out 
man thread  tid is 28244 man pid is 28244
go into threadfunc ,pthread_self id is -1511180544
go into  threadfunc ,gettid id is 28245 
end and get the tgkill tid is -1
go into threadfunc ,pthread_self id is -1527965952
go into threadfunc ,pthread_self id is -1519573248
go into  threadfunc ,gettid id is 28246 
go into  threadfunc ,gettid id is 28247 

同時使用命令查看得到如下輸出

[root@node1 ~]# ps -ef|grep a.out
root     28244 21581  0 09:41 pts/10   00:00:00 ./a.out
root     28902  8965  0 09:41 pts/7    00:00:00 grep --color=auto a.out
[root@node1 ~]# ps -Tp 28244  PID  SPID TTY          TIME CMD
28244 28244 pts/10   00:00:00 a.out
28244 28245 pts/10   00:00:00 a.out
28244 28246 pts/10   00:00:00 a.out
28244 28247 pts/10   00:00:00 a.out

命令ps -Tp pid同樣可以得到當前進程相關的信息,第二列SPID即為線程id.可以看到進程id和主線程id是一致的,同時pthread_self函數的posix 線程id并非我們實際真實的線程id

總結

以上是生活随笔為你收集整理的ceph bluestore源码分析:C++ 获取线程id的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。