乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C++ 中怎樣在一個(gè)類(lèi)對(duì)象中使用多線程

       禁忌石 2017-06-05
      我是在一個(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è)線程。
      *************************************************************************************

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多