在使用利用CreateThread創建線程時
struct A
{
DWORD WINAPI MyThreadFunction(LPVOID) {}
void Run()
{
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function name
0, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
}
};
visual studio 報了如下錯誤:
英文環境
E0167 argument of type "DWORD (__stdcall A::*)()" is incompatible with parameter of type "LPTHREAD_START_ROUTINE"
中文環境
E0167 DWORD (__stdcall A::*)類型的實參與“LPTHREAD_START_ROUTINE”類型的形參不兼容
問題解決辦法參考文檔,將MyThreadFunction
函數訂單改為DWORD static WINAPI MyThreadFunction()
原因在ThreadProc callback function有描述
Each thread receives a unique copy of the local variables of this function. Any static or global variables are shared by all threads in the process.