这是用例:
Log(char* s); // prints out a log message
现在:
Log("hello world\n"); // called from Thread1
期望的输出:
Thread1: hello world
现在:
Log("hello world\n"); // called from Thread2
期望的输出:
Thread2: hello world
我可以有一个将线程pid映射到字符串的映射。然而,我需要的是一个函数,当调用不同的线程时,它给我唯一的识别标签。这可能吗?
谢谢!
(Langauge:C / C ++)
答案 0 :(得分:6)
您需要将pthread_self()
传递到Log()函数(或编写宏)。
答案 1 :(得分:1)
一种干净的方法是为线程名创建一个线程属性(使用pthread_attr_ *),在每个线程中设置属性,然后在日志函数中读取它。
你也可以在日志功能中使用pthread_self。