• 在VC中使用线程


    下面是在重载了MFC中的线程操作类,这样的操作是线程安全的

    #include <afxwin.h>
    #include <iostream.h>

    class CUserThread : public CWinThread
    {
    public:
     CUserThread(AFX_THREADPROC ThreadProc);
     static UINT ThreadFunc(LPVOID lparam);
    public:
     int startcounter ;
    private:
     virtual void GO();
    };

    CUserThread::CUserThread(AFX_THREADPROC ThreadProc):CWinThread(ThreadProc , NULL)
    {
     m_bAutoDelete = false ;
     m_pThreadParams = this ;
    }

    UINT CUserThread::ThreadFunc(LPVOID lparams)
    {
      CUserThread * userThread = (CUserThread *) lparams ;
      userThread->GO();
      return 0 ;
    }
    void CUserThread::GO()
    {
      for(int i=0;i<10 ;i++)
      {
        printf("%d\n" , i);
     Sleep(1000);
      }
     
    }
    void main()
    {
      CUserThread * pUserThread[5];

      for(int i=0;i<5;i++)
      {
       pUserThread[i] = new CUserThread(CUserThread::ThreadFunc);
       pUserThread[i]->CreateThread();
      }

      for(int j=0;i<5;i++)
      {
        WaitForSingleObject(pUserThread[j]->m_hThread , INFINITE);
     delete pUserThread[j] ;
      }

    }

    转自:http://zhouweigang01.blog.163.com/blog/#m=0&t=1&c=fks_083066081085080067082083074074087082081074084081

  • 相关阅读:
    UVa-129
    UVa-524
    有点迷茫
    北邮之行~
    UVa-253
    心累--期末考试成绩
    UVa-220 Othello
    UVa-201 Squares
    UVA-1589 Xiangqi
    UVa-213 Message Decoding
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699900.html
Copyright © 2020-2023  润新知