c++ - 使用 pThreads,是否可以编写一个函数来检测从哪个线程调用它?

标签 c++ c multithreading pthreads

这是用例:

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

我可以有一个将线程 pids 映射到字符串的映射。然而,我需要的是一个函数,当从不同的线程调用它时,它会给我唯一的识别标签。这可能吗?

谢谢!

(语言:C/C++)

最佳答案

您需要将 pthread_self() 传递给您的 Log() 函数(或编写一个宏)。

关于c++ - 使用 pThreads,是否可以编写一个函数来检测从哪个线程调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2195760/

相关文章:

c++ - 未启动事件循环时不优雅/杀死 Qt 应用程序

C语言 : How is it possible for your program to continue running for a little bit after an "assert()" has failed?

c++ - boost::thread join 函数 block 调用线程

c++ - 当另一个线程正在运行时,线程连接会导致段错误

c# - 在 C# Winforms 中,让异步方法保证其在 UI 线程上执行的最精确方法是什么?

c++ - WSAStringToAddress 认为 "1.2.3"是有效的 IP 地址

c++ - 使用 Visual Studio 2019 和 cmake for x86 编译 pe-parse 库

c++ - DetourAttach(&(PVOID &)BindKeyT, BindKeyD); 是什么意思?意思是?将绕行附加到内存地址

c - C99 标准是否允许编译器转换代码,以便在满足某些推导条件后不再评估相同的表达式?

c - C 中的 XOR 运算符