本文介紹了您如何將 CreateThread 用于屬于類成員的函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何使用 CreateThread()
創建屬于類成員的函數的線程?
How do you use CreateThread()
to create threads of functions which are class members?
推薦答案
你需要創建一個靜態方法作為實際的線程啟動函數,并傳遞一個指向實例的指針作為lpParameter
CreateThread
的參數.這將傳遞給靜態方法,該方法可以將其轉換為對象指針并調用成員函數.
You need to create a static method to use as the actual thread start function, and pass a pointer to the instance as the lpParameter
argument to CreateThread
. That will get passed to the static method, which can cast it to an object pointer and call through to the member function.
class MyClass
{
static DWORD WINAPI StaticThreadStart(void* Param)
{
MyClass* This = (MyClass*) Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
// Do stuff
}
void startMyThread()
{
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
}
};
這篇關于您如何將 CreateThread 用于屬于類成員的函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!