C++多线程代码范例剖析

C++编程语言中支持多线程运行。那么如何才能正确的实现这一功能呢?今天我们就在这里先通过一个带来范例来详细解读C++多线程的应用方式,希望初学者们可以根据我们介绍的内容从中学到一些知识。

创新互联是网站建设技术企业,为成都企业提供专业的网站建设、成都做网站,网站设计,网站制作,网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制适合企业的网站。10多年品质,值得信赖!

C++多线程应用示例:

主线程创建2个线程t1和t2,创建时2个线程就被挂起,后来调用ResumeThread恢复2个线程,是其开始执行,调用WaitForSingleObject等待2个线程执行完,然后推出主线程即结束进程。

 
 
 
  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++获得系统时间不同方案介绍
  2. C++静态成员函数基本概念讲解
  3. C++静态数据成员定义及应用浅谈
  4. C++指针重载应用代码解读
  5. C++模板函数重载不同之处点评

当前题目:C++多线程代码范例剖析
分享路径:http://www.hantingmc.com/qtweb/news7/263157.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联