我有以下内容:
class DThread
{
virtual void run()=0;
_beginthreadex(NULL,0,tfunc,this,0,&m_UIThreadID); // class itself being passed as param to thread function...
static unsigned int __stdcall tfunc(void* thisptr)
{
static_cast<DThread*>(thisptr)->run();
return 0;
}
//other stuff
}
run 函数是在派生类中实现的。
为什么在线程中调用的函数是通过强制转换的 this
指针调用的?这是好的做法吗?
直接调用不行吗?
实际需要运行的函数在派生类中。
我的问题是
最佳答案
_beginthreadex
需要一个 (stdcall) C 风格函数,它不能使用 C++ 成员函数,因为它不了解 C++。使成员函数运行的方法是传递一个指向对象的指针并在该函数内部调用成员函数。这种功能通常被称为蹦床。
关于c++ - 奇怪的 C++ 线程函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2848928/