muduo源码解析3-currentthread命名空间
CurrentThread
作用:
CurrentThread并不是一个类,而是一个命名空间,在mymuduo内部,目的是提供对于当前线程的管理操作。
内部变量:
__thread int t_cachedTid; //当前线程ID __thread char t_tidString[32];//当前线程ID ,char*类型 __thread int t_tidStringLength;//char*大小 __thread const char* t_threadName;//当前线程名字
__thread 修饰变量每一个线程有一份独立实体,各个线程的值互不干扰。也就是说,每个线程都独立的拥有这四个变量。
内部函数:
//返回线程ID inline int tid() { //__builtin_expect(EXP, N)。意思是:EXP==N的概率很大。 if(__builtin_expect(t_cachedTid==0,0)) cacheTid(); return t_cachedTid; } //返回线程ID,char*类型 inline const char* tidString() { return t_tidString; } //返回char*类型线程ID的大小 inline int tidStringLength() { return t_tidStringLength; } //返回线程的名字 inline const char* name() { return t_threadName; } bool isMainThread(); void sleepUsec(int64_t usec); //返回线程调用栈信息, string stackTrace(bool demangle);
stackTrace获取的信息如下
/*
打印信息是这么个玩意儿。stackTrace内部实现没有去深究。
/home/zqc/c_c++Repo/Qtcode/build-mymuduo-Desktop_Qt_5_14_2_GCC_64bit-Debug/mymuduo() [0x401dd6]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f8c8c6d5840]
/home/zqc/c_c++Repo/Qtcode/build-mymuduo-Desktop_Qt_5_14_2_GCC_64bit-Debug/mymuduo() [0x4012e9]
*/
测试:
了解一下__thread声明变量的用法,每个线程都独立拥有一个该变量。