偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C++多線程代碼范例剖析

開發(fā) 后端
在這篇文章中,我們通過在主線程中創(chuàng)建兩個(gè)線程的方法來為大家詳細(xì)解讀C++多線程的具體應(yīng)用方法,以方便大家將來的實(shí)際應(yīng)用。

C++編程語(yǔ)言中支持多線程運(yùn)行。那么如何才能正確的實(shí)現(xiàn)這一功能呢?今天我們就在這里先通過一個(gè)帶來范例來詳細(xì)解讀C++多線程的應(yīng)用方式,希望初學(xué)者們可以根據(jù)我們介紹的內(nèi)容從中學(xué)到一些知識(shí)。

C++多線程應(yīng)用示例:

主線程創(chuàng)建2個(gè)線程t1和t2,創(chuàng)建時(shí)2個(gè)線程就被掛起,后來調(diào)用ResumeThread恢復(fù)2個(gè)線程,是其開始執(zhí)行,調(diào)用WaitForSingleObject等待2個(gè)線程執(zhí)行完,然后推出主線程即結(jié)束進(jìn)程。

  1. #include <stdio.h> 
  2. #include <string> // for STL string class  
  3. #include <windows.h> // for HANDLE  
  4. #include <process.h> // for _beginthread()  
  5. using namespace std;  
  6. class ThreadX  
  7. {  
  8. private:  
  9. int loopStart;  
  10. int loopEnd;  
  11. int dispFrequency;  
  12. public:  
  13. string threadName;  
  14. ThreadX( int startValue, int endValue, int frequency )  
  15. {  
  16. loopStart = startValue;  
  17. loopEnd = endValue;  
  18. dispFrequency = frequency;  
  19. }  
  20. static unsigned __stdcall ThreadStaticEntryPoint(void * pThis)  
  21. {  
  22. ThreadX * pthX = (ThreadX*)pThis; // the tricky cast  
  23. pthX->ThreadEntryPoint(); // now call the true entry-point-function
  24. return 1; // the thread exit code  
  25. }  
  26. void ThreadEntryPoint()  
  27. {  
  28. for (int i = loopStart; i <= loopEnd; ++i)  
  29. {  
  30. if (i % dispFrequency == 0)  
  31. {  
  32. printf( "%s: i = %d\n", threadName.c_str(), i );  
  33. }  
  34. }  
  35. printf( "%s thread terminating\n", threadName.c_str() );  
  36. }  
  37. };  
  38. int main()  
  39. {  
  40. ThreadX * o1 = new ThreadX( 0, 1, 2000 );  
  41. HANDLE hth1;  
  42. unsigned uiThread1ID;  
  43. hth1 = (HANDLE)_beginthreadex( NULL, // security  
  44. 0, // stack size  
  45. ThreadX::ThreadStaticEntryPoint,  
  46. o1, // arg list  
  47. CREATE_SUSPENDED, // so we can later call ResumeThread()  
  48. &uiThread1ID );  
  49. if ( hth1 == 0 )  
  50. printf("Failed to create thread 1\n");  
  51. DWORD dwExitCode;  
  52. GetExitCodeThread( hth1, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259  
  53. printf( "initial thread 1 exit code = %u\n", dwExitCode );  
  54. o1->threadName = "t1";  
  55. ThreadX * o2 = new ThreadX( -1000000, 0, 2000 );  
  56. HANDLE hth2;  
  57. unsigned uiThread2ID;  
  58. hth2 = (HANDLE)_beginthreadex( NULL, // security  
  59. 0, // stack size  
  60. ThreadX::ThreadStaticEntryPoint,  
  61. o2, // arg list  
  62. CREATE_SUSPENDED, // so we can later call ResumeThread()  
  63. &uiThread2ID );  
  64. if ( hth2 == 0 )  
  65. printf("Failed to create thread 2\n");  
  66. GetExitCodeThread( hth2, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259  
  67. printf( "initial thread 2 exit code = %u\n", dwExitCode );  
  68. o2->threadName = "t2";  
  69. ResumeThread( hth1 ); // serves the purpose of Jaeschke's t1->Start()  
  70. ResumeThread( hth2 );  
  71. WaitForSingleObject( hth1, INFINITE );  
  72. WaitForSingleObject( hth2, INFINITE );  
  73. GetExitCodeThread( hth1, &dwExitCode );  
  74. printf( "thread 1 exited with code %u\n", dwExitCode );  
  75. GetExitCodeThread( hth2, &dwExitCode );  
  76. printf( "thread 2 exited with code %u\n", dwExitCode );  
  77. CloseHandle( hth1 );  
  78. CloseHandle( hth2 );  
  79. delete o1;  
  80. o1 = NULL;  
  81. delete o2;  
  82. o2 = NULL;  
  83. printf("Primary thread terminating.\n");  
  84. }

以上就是對(duì)C++多線程的相關(guān)介紹。

【編輯推薦】

  1. C++獲得系統(tǒng)時(shí)間不同方案介紹
  2. C++靜態(tài)成員函數(shù)基本概念講解
  3. C++靜態(tài)數(shù)據(jù)成員定義及應(yīng)用淺談
  4. C++指針重載應(yīng)用代碼解讀
  5. C++模板函數(shù)重載不同之處點(diǎn)評(píng)
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2012-05-18 10:36:20

CC++編程

2011-07-21 11:12:58

iPhone 線程 多線程

2023-08-02 09:29:40

任務(wù)池TaskPool

2023-08-01 16:35:48

鴻蒙ArkUI應(yīng)用開發(fā)

2010-02-03 10:05:48

C++ enum枚舉

2010-01-18 14:09:58

C++多線程

2010-02-05 15:30:54

C++多線程測(cè)試

2021-02-25 15:58:46

C++線程編程開發(fā)技術(shù)

2021-03-05 07:38:52

C++線程編程開發(fā)技術(shù)

2010-02-04 13:45:36

C++類模板

2010-02-01 14:26:50

C++讀寫文本文件

2024-06-24 08:10:00

C++互斥鎖

2010-01-14 17:42:47

CC++

2015-04-17 10:35:51

c++c++程序內(nèi)存泄漏檢測(cè)代碼

2010-02-03 10:17:29

C++繼承方式

2010-01-28 16:31:54

C++類型

2023-12-14 15:05:08

volatile代碼C++

2024-11-05 16:29:57

2010-01-12 15:03:33

C++代碼

2025-05-06 09:12:46

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)