国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C++ 中怎樣在一個(gè)類對(duì)象中使用多線程
我是在一個(gè)類中定義兩個(gè)函數(shù),然后在main函數(shù)中創(chuàng)建兩個(gè)線程,而線程的兩個(gè)操作就是那個(gè)類對(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); 這樣寫也不對(duì)
    while(true)
    {
        Sleep(100);
    }
    return 0;
}

****************************************************************************
(對(duì)CreateThread來說,)作為線程函數(shù)的函數(shù)如果是類成員,則必須為靜態(tài)成員。
將線程傳入的函數(shù)設(shè)為static,網(wǎng)上還有一種解決方法,具體忘了,有興趣可以查查

******************************************************************************
一般來說是使用一個(gè)公共的線程入口,再通過lpParameter參數(shù)來做具體的工作,比如:
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;
}

***************************************************************
直接用類的成員函數(shù)綁定即可。前提是得啟用c++11的std::thread
****************************************************************
一般來說是使用一個(gè)公共的線程入口,再通過lpParameter參數(shù)來做具體的工作,比如:
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;
}
**************************************************************************************************

一般來說是使用一個(gè)公共的線程入口,再通過lpParameter參數(shù)來做具體的工作,比如:
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;
}
謝謝 ,非常感謝! 我想問個(gè)問題,如果說一個(gè)類中有成員變量,這個(gè)會(huì)影響嗎?

************************************************************************************
不會(huì)。如果你有條件使用C++11編譯器,也可以使用std::thread,基本思路都是一樣的

****************************************************************************************
使用類的靜態(tài)成員函數(shù)作為線程的過程函數(shù),這樣避免了對(duì)象的this指針問題

************************************************************************************

CreateThread的線程入口函數(shù)不能是一個(gè)類成員函數(shù)。不過可以使用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();
 }

************************************************************************************
使用類的靜態(tài)成員函數(shù)作為線程的過程函數(shù),這樣避免了對(duì)象的this指針問題
我也了解過了,可是我設(shè)計(jì)的那個(gè)東西,必須要和對(duì)象有關(guān)聯(lián)。也就是說一個(gè)對(duì)象必須要運(yùn)行幾個(gè)線程。
*************************************************************************************

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服