日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++多線程代碼范例剖析

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

創(chuàng)新互聯(lián)是網(wǎng)站建設技術企業(yè),為成都企業(yè)提供專業(yè)的網(wǎng)站建設、成都做網(wǎng)站,網(wǎng)站設計,網(wǎng)站制作,網(wǎng)站改版等技術服務。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。10多年品質,值得信賴!

C++多線程應用示例:

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

 
 
 
  1. #include  
  2. #include  // for STL string class  
  3. #include  // for HANDLE  
  4. #include  // 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. }

以上就是對C++多線程的相關介紹。

【編輯推薦】

  1. C++獲得系統(tǒng)時間不同方案介紹
  2. C++靜態(tài)成員函數(shù)基本概念講解
  3. C++靜態(tài)數(shù)據(jù)成員定義及應用淺談
  4. C++指針重載應用代碼解讀
  5. C++模板函數(shù)重載不同之處點評

分享題目:C++多線程代碼范例剖析
網(wǎng)頁URL:http://m.5511xx.com/article/dpsocdp.html