伊莉討論區

標題: 想請問class底下建立執行序的問題(已解決) [打印本頁]

作者: baepi    時間: 2020-10-4 12:30 AM     標題: 想請問class底下建立執行序的問題(已解決)

本帖最後由 baepi 於 2020-10-23 05:20 PM 編輯
  1. class Test
  2. {
  3.       void A();
  4.       void B();
  5. };
複製代碼
舉個簡單的例子,由A程式使用_beginthreadex這類的函式呼叫B我知道大多數人會說,只要在B前面添加static即可
但若是如此,那使用class就變得毫無意義了
因為我希望每個class都能新增一個執行序,而且彼此資料都能保存於class內並不會相互干擾
說白一點,使用static的缺點如下
  1. class Test
  2.         {
  3.         public:
  4.                 Test();
  5.                 static int c;
  6.                 static int cc();
  7.         };
  8.         Test::Test()
  9.         {
  10.         }
  11.         int Test::cc()
  12.         {
  13.                 return c;
  14.         }
  15.         int Test::c = 0;
  16.         
  17.         int main()
  18.         {
  19.                 Test a, b;
  20.                 cout << a.cc() << "\t" << b.cc() << endl;
  21.                 b.c = 2;
  22.                 cout << a.cc() << "\t" << b.cc() << endl;
  23.                 system("pause");
  24.         }
複製代碼
物件A與物件B,之間的數據C已經相互關聯(理所當然)
那麼,既然資料都相互關聯,就無法滿足我的需求了

希望有大大能給我提示或建議,在此先感謝點進來想要幫助小弟的各位大大了

作者: tryit244178    時間: 2020-10-6 01:45 PM

本帖最後由 tryit244178 於 2020-10-7 05:03 AM 編輯

參考參考
  1. #include <iostream>
  2. #include <process.h>
  3. #include <windows.h>

  4. using namespace std;

  5. class Test{
  6.         HANDLE hThread;
  7.         unsigned threadID;
  8.         
  9. public:
  10.         int c = 0;
  11.         
  12.         Test(){}
  13.         
  14.         int cc()
  15.         {
  16.                 return c;
  17.         }
  18.         
  19.         void startThread()
  20.         {
  21.                 hThread = (HANDLE)_beginthreadex(NULL, 0, &Test::BBB, this, 0, &threadID);
  22.         }
  23.         
  24. private:
  25.         void AAA()
  26.         {
  27.                 cout << "do something" << endl;
  28.                 c = 55;
  29.                 _endthreadex( 0 );
  30.         }
  31.         
  32.         static unsigned int __stdcall BBB(void* p_this)
  33.         {
  34.                 Test* p_Test = static_cast<Test*>(p_this);
  35.                 p_Test->AAA();
  36.                
  37.                 WaitForSingleObject( p_Test->hThread, INFINITE );
  38.                 CloseHandle( p_Test->hThread );
  39.                 return 0;
  40.         }
  41. };
  42.       
  43. int main()
  44. {
  45.         Test a, b;
  46.         cout << a.cc() << "\t" << b.cc() << endl;
  47.         b.c = 2;
  48.         a.startThread();
  49.         cout << a.cc() << "\t" << b.cc() << endl;
  50.         
  51.         return 0;
  52. }
複製代碼

作者: baepi    時間: 2020-10-6 04:58 PM

tryit244178 發表於 2020-10-6 01:45 PM
參考參考

大大裡面的BBB這寫法...果然高手,小弟真是沒有想到

只是好奇想請教,該執行序結束前的
  1. WaitForSingleObject(p_Test->hThread, INFINITE);
  2.                 CloseHandle(p_Test->hThread);
複製代碼
我楞是看不出用意?
在這等待的意思是???
甚至還在裡面關閉???
這兩個動作不應該是放在主程式動作的嗎?
能否請大大稍微指點指點,讓小弟學學知識
作者: tryit244178    時間: 2020-10-6 06:02 PM

本帖最後由 tryit244178 於 2020-10-10 02:01 PM 編輯
baepi 發表於 2020-10-6 04:58 PM
大大裡面的BBB這寫法...果然高手,小弟真是沒有想到

只是好奇想請教,該執行序結束前的我楞是看不出用意 ...

你在類別裡建立的資料,就要在類別結束前把它釋放掉
如果沒有等待執行緒執行完的話,執行緒會馬上被第40行釋放掉

補充內容 (2020-10-7 04:11 AM):
啊…那兩行要移到startThread裡才是。沒注意到,抱歉
作者: cockroachrun    時間: 2020-10-23 03:44 PM

本帖最後由 cockroachrun 於 2020-10-23 03:47 PM 編輯

c++11 版之後 有 std::thread . 不用搞這麼麻煩. 所有該弄的複雜的事. 他都幫你搞定了
  1. #include <thread>
  2. #include <iostream>
  3. class AA
  4. {
  5. public:
  6.         AA(int aa) : a(aa)
  7.         {

  8.         }


  9.         void threadEntry(int x)
  10.         {
  11.                 std::cout << "x = " << x << std::endl;
  12.                 std::cout << "class member data a = " << a << std::endl;
  13.                 for (int i = 0; i < x; ++i)
  14.                 {
  15.                         std::cout << i << std::endl;
  16.                 }
  17.         }

  18. private:
  19.         int a;
  20. };


  21. int main()
  22. {
  23.         AA a1(1);
  24.         AA a2(2);

  25.         std::thread th0(std::bind(&AA::threadEntry, &a1, 10));
  26.         std::thread th1(std::bind(&AA::threadEntry, &a2, 20));

  27.         th0.join();
  28.         th1.join();
  29.         
  30.         return 0;
  31. }
複製代碼





歡迎光臨 伊莉討論區 (http://www44.eyny.com/) Powered by Discuz!