本文介紹了您如何將 CreateThread 用于屬于類成員的函數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何使用 CreateThread()
創(chuàng)建屬于類成員的函數(shù)的線程?
How do you use CreateThread()
to create threads of functions which are class members?
推薦答案
你需要創(chuàng)建一個靜態(tài)方法作為實際的線程啟動函數(shù),并傳遞一個指向?qū)嵗闹羔樧鳛?code>lpParameterCreateThread
的參數(shù).這將傳遞給靜態(tài)方法,該方法可以將其轉(zhuǎn)換為對象指針并調(diào)用成員函數(shù).
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);
}
};
這篇關(guān)于您如何將 CreateThread 用于屬于類成員的函數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!