我是在一個(gè)類(lèi)中定義兩個(gè)函數(shù),然后在main函數(shù)中創(chuàng)建兩個(gè)線程,而線程的兩個(gè)操作就是那個(gè)類(lèi)對(duì)象的兩個(gè)函數(shù)。發(fā)現(xiàn)不管怎么樣都是錯(cuò)誤。錯(cuò)誤代碼為“函數(shù)調(diào)用缺少參數(shù)列表,請(qǐng)使用XXX創(chuàng)建指向成員的指針?!?/div> C/C++ code #include <Windows.h> class Object { DWORD WINAPI fun1(LPVOID lpParam); DWORD WINAPI fun2(LPVOID lpParam); }; DWORD WINAPI Object::fun1(LPVOID lpParam) { // do something return 0; } DWORD WINPAI Object::fun2(LPVOID lpParam) { // do something return 0; } int main() { Object o; HANDLE hThread1 = CreateThread(NULL, 0, o.fun1(), NULL, 0, NULL); HANDLE hThread2 = CreateThread(NULL, 0, o.fun2(), NULL, 0, NULL); // HANDLE hThread1 = CreateThread(NULL, 0, o.fun1, NULL, 0, NULL); 這樣寫(xiě)也不對(duì) while(true) { Sleep(100); } return 0; } **************************************************************************** (對(duì)CreateThread來(lái)說(shuō),)作為線程函數(shù)的函數(shù)如果是類(lèi)成員,則必須為靜態(tài)成員。 將線程傳入的函數(shù)設(shè)為static,網(wǎng)上還有一種解決方法,具體忘了,有興趣可以查查 ****************************************************************************** 一般來(lái)說(shuō)是使用一個(gè)公共的線程入口,再通過(guò)lpParameter參數(shù)來(lái)做具體的工作,比如: C/C++ code #include <Windows.h> #include <functional> DWORD WINAPI ThreadDummy(LPVOID pCallable) { auto fun = static_cast<std::function<DWORD(void)>*>(pCallable); DWORD ret = (*fun)(); delete fun; return ret; } struct Object { DWORD WINAPI fun1(); DWORD WINAPI fun2(); }; DWORD WINAPI Object::fun1() { // do something return 0; } DWORD WINAPI Object::fun2() { // do something return 0; } int main() { Object o; HANDLE hThread1 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun1, &o)), 0, NULL); HANDLE hThread2 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun2, &o)), 0, NULL); while(true) { Sleep(100); } return 0; } *************************************************************** 直接用類(lèi)的成員函數(shù)綁定即可。前提是得啟用c++11的std::thread **************************************************************** 一般來(lái)說(shuō)是使用一個(gè)公共的線程入口,再通過(guò)lpParameter參數(shù)來(lái)做具體的工作,比如: C/C++ code #include <Windows.h> #include <functional> DWORD WINAPI ThreadDummy(LPVOID pCallable) { auto fun = static_cast<std::function<DWORD(void)>*>(pCallable); DWORD ret = (*fun)(); delete fun; return ret; } struct Object { DWORD WINAPI fun1(); DWORD WINAPI fun2(); }; DWORD WINAPI Object::fun1() { // do something return 0; } DWORD WINAPI Object::fun2() { // do something return 0; } int main() { Object o; HANDLE hThread1 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun1, &o)), 0, NULL); HANDLE hThread2 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun2, &o)), 0, NULL); while(true) { Sleep(100); } return 0; } ************************************************************************************************** 一般來(lái)說(shuō)是使用一個(gè)公共的線程入口,再通過(guò)lpParameter參數(shù)來(lái)做具體的工作,比如: C/C++ code #include <Windows.h> #include <functional> DWORD WINAPI ThreadDummy(LPVOID pCallable) { auto fun = static_cast<std::function<DWORD(void)>*>(pCallable); DWORD ret = (*fun)(); delete fun; return ret; } struct Object { DWORD WINAPI fun1(); DWORD WINAPI fun2(); }; DWORD WINAPI Object::fun1() { // do something return 0; } DWORD WINAPI Object::fun2() { // do something return 0; } int main() { Object o; HANDLE hThread1 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun1, &o)), 0, NULL); HANDLE hThread2 = CreateThread(NULL, 0, ThreadDummy, new std::function<DWORD(void)>(std::bind(&Object::fun2, &o)), 0, NULL); while(true) { Sleep(100); } return 0; } 謝謝 ,非常感謝! 我想問(wèn)個(gè)問(wèn)題,如果說(shuō)一個(gè)類(lèi)中有成員變量,這個(gè)會(huì)影響嗎? ************************************************************************************ 不會(huì)。如果你有條件使用C++11編譯器,也可以使用std::thread,基本思路都是一樣的 **************************************************************************************** 使用類(lèi)的靜態(tài)成員函數(shù)作為線程的過(guò)程函數(shù),這樣避免了對(duì)象的this指針問(wèn)題 ************************************************************************************ CreateThread的線程入口函數(shù)不能是一個(gè)類(lèi)成員函數(shù)。不過(guò)可以使用std::thread實(shí)現(xiàn),需要VS2012或以上的編譯器支持 C/C++ code #include <thread> #include <iostream> using namespace std; class MyClass { public: MyClass(int n) : m_n(n){} void Run() { cout << m_n << endl; } int m_n; }; void main() { MyClass test(5); thread th(&MyClass::Run, &test); th.join(); } ************************************************************************************ 使用類(lèi)的靜態(tài)成員函數(shù)作為線程的過(guò)程函數(shù),這樣避免了對(duì)象的this指針問(wèn)題 我也了解過(guò)了,可是我設(shè)計(jì)的那個(gè)東西,必須要和對(duì)象有關(guān)聯(lián)。也就是說(shuō)一個(gè)對(duì)象必須要運(yùn)行幾個(gè)線程。 ************************************************************************************* |
|