c ++识别函数中的当前线程?

Mat*_*Mat 0 c++ windows mfc multithreading

假设我有一个带有静态函数的单例对象:

static int MySingletonObject::getInt()
Run Code Online (Sandbox Code Playgroud)

现在我想根据调用函数的工作线程(MFC 线程)返回不同的 int。

我知道我可以在创建线程时将参数传递给线程函数。但是有没有办法在没有这些参数信息的情况下识别线程?

谢谢!

sha*_*oth 5

您可以调用GetCurrentThreadId() - 将返回一个整数标识符 - 或GetCurrentThread() - 将返回一个可以转换为整数标识符的句柄 - 从任何线程 - 这些值对于进程中的任何线程都是唯一的。

  • 这是不可能的!,Windows 将此信息保存在内存结构 http://en.wikipedia.org/wiki/Win32_Thread_Information_Block 中,因此它应该与从内存中获取整数值一样昂贵(应该非常便宜) (2认同)