最近做一个跨平台的项目,需支持Windows,GUN Linux平台。
线程使用了Pthread线程,其中有一个日志模块中需要打印线程ID,做一些调试。
众所周知获取线程ID的函数:pthread_self(),返回一个pthread_t类型的线程标识符。
Open Source POSIX Threads for Win32
pthread.h
/*
* Generic handle type - intended to extend uniqueness beyond
* that available with a simple pointer. It should scale for either
* IA-32 or IA-64.
*/
typedef struct {
void * p; /* Pointer to actual object */
unsigned int x; /* Extra information - reuse count etc */
} ptw32_handle_t;
typedef ptw32_handle_t pthread_t;
这里的pthread_t是一个结构体,查看源代码可以得知,成员void *p其实是保存了CreateThread返回的WIN32线程句柄。