Linux 线程控制函数
一、线程函数
1. 线程 ID
获取线程 ID,线程 ID 的名字叫做 tid。
#include <pthread.h>
pthread_t pthread_self(void);
返回值:该函数返回调用它的线程的线程 tid,返回类型为 pthread_t。
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <thread>
#include <string>
using namespace std;
// 将数字转为十六进制
string toHex(int num) {
char ret[64];
snprintf(ret, sizeof(ret), "%p", num);
return ret;
}
// 循环打印 tid
void* threadroutine(void* args) {
while (true) {
sleep(2);
cout << "thread id: " << toHex(pthread_self()) << endl;
}
;
}
{
tid;
(&tid, , threadroutine, );
() {
cout << << (tid) << endl;
();
}
(tid, );
;
}








