c++多线程(三)

多线程实例4

此实例演示你的机器最多能创建多少个线程。此实例程序相当简单。

线程函数:

[cpp]  view plain copy
  1. volatile BOOL m_bRUN = TRUE; //表示是否能继续添加线程  
  2. DWORD WINAPI ThreadProc(LPVOID lpParam)  
  3. {  
  4.     while (m_bRUN)  
  5.     {  
  6.         Sleep(2000);  
  7.     }  
  8.     return 0;  
  9. }  

   主线程函数:

  

[cpp]  view plain copy
  1. void CMthread4Dlg::OnStart()   
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     DWORD threadID;  
  5.     long nCount = 0;  
  6.     m_nCount = 0;  
  7.     UpdateData(FALSE);  
  8.     GetDlgItem(IDC_START)->EnableWindow(FALSE);  
  9.   
  10.   
  11.     while (m_bRUN)  
  12.     {  
  13.         if (CreateThread(NULL,0,ThreadProc,NULL,0,&threadID) == NULL)  
  14.         {  
  15.             m_bRUN = FALSE;  
  16.             break;  
  17.         }  
  18.         else  
  19.         {  
  20.             nCount++;  
  21.         }  
  22.     }  
  23.     m_nCount = nCount;  
  24.     UpdateData(FALSE);  
  25.     Sleep(5000);  
  26.     GetDlgItem(IDC_START)->EnableWindow(TRUE);  
  27.     m_bRUN = TRUE;  
  28. }  

说明:当m_bRUN一直为TRUE的时候,程序一直创建线程,直至创建的线程到达最大值。

 执行结果:



多线程实例五


此实例演示,创建了一个线程类,线程类继承自CWinThread。主线程通过AfxBeginThread函数调用线程类。

       

       主线程调用线程类:

[cpp]  view plain copy
  1. void CMthread6Dlg::OnUiThread()   
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     CWinThread *pThread = AfxBeginThread(RUNTIME_CLASS(CUIThread));  
  5. }  
     线程类:

头文件中类定义:

    class CMthread6Dlg : public CDialog

    线程类:

    

[cpp]  view plain copy
  1. // UIThread.cpp : implementation file  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Mthread6.h"  
  6. #include "UIThread.h"  
  7.   
  8. #ifdef _DEBUG  
  9. #define new DEBUG_NEW  
  10. #undef THIS_FILE  
  11. static char THIS_FILE[] = __FILE__;  
  12. #endif  
  13.   
  14. /  
  15. // CUIThread  
  16.   
  17. IMPLEMENT_DYNCREATE(CUIThread, CWinThread)  
  18.   
  19. CUIThread::CUIThread()  
  20. {  
  21. }  
  22.   
  23. CUIThread::~CUIThread()  
  24. {  
  25. }  
  26.   
  27. BOOL CUIThread::InitInstance()  
  28. {  
  29.     // TODO:  perform and per-thread initialization here  
  30.     m_dlg.Create(IDD_UITHREADDLG);  
  31.     m_dlg.ShowWindow(SW_SHOW);  
  32.     m_pMainWnd = &m_dlg;  
  33.     return TRUE;  
  34. }  
  35.   
  36. int CUIThread::ExitInstance()  
  37. {  
  38.     // TODO:  perform any per-thread cleanup here  
  39.     m_dlg.DestroyWindow();  
  40.     return CWinThread::ExitInstance();  
  41. }  
  42.   
  43. BEGIN_MESSAGE_MAP(CUIThread, CWinThread)  
  44.     //{{AFX_MSG_MAP(CUIThread)  
  45.         // NOTE - the ClassWizard will add and remove mapping macros here.  
  46.     //}}AFX_MSG_MAP  
  47. END_MESSAGE_MAP()  
  48.   
  49. /  
  50. // CUIThread message handlers  
从上边的代码中,自建的线程类CUIThread重写了CWinThread类的InitInstance和ExitInstance函数

  InitInstance函数中,创建了一个对话框。

   运行结果:

   



源代码下载地址:

    http://download.csdn.net/detail/richerg85/4218429

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值