Windows SDK Thread: How to create a worker thread?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: Windows SDK Thread: How to create a worker thread?

Hybrid View

  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Threads: How to create a worker thread?

    Q: How to create a worker thread?

    A: There are several ways to create a worker thread:

    • '_beginthread()' (C run-time library)
    • '_beginthreadex()' (C run-time library)
    • 'CreateThread()' (Win32 API)
    • 'CreateRemoteThread()' (Win32 API)
    • 'AfxBeginThread()' (MFC)

    The following samples will show the creation of a thread using the three functions '_beginthreadex()', 'CreateThread()' and 'AfxBeginThread()'.


    • '_beginthreadex()'

      Code:
      class CFoo
      {
      public:
        CFoo()
        {
          m_uiThreadID     = 0;
          m_ulThreadHandle = 0;
        }
                  
        bool Create()
        {
          m_ulThreadHandle = _beginthreadex(0,
                                            0,
                                            ThreadFunc,
                                            this,
                                            0,
                                            &m_uiThreadID);
          if(!m_ulThreadHandle)
          {
            // Could not create thread
            return false;
          }
          return true;              
        }
                  
      private:
        unsigned int  m_uiThreadID;
        unsigned long m_ulThreadHandle;
                  
        static unsigned int __stdcall ThreadFunc(void *pvParam);
      };
    • 'CreateThread()'

      Code:
      class CFoo
      {
      public:
        CFoo()
        {
          m_dwThreadID = 0;
          m_hThread    = 0;
        }
                    
        ~CFoo() { CloseHandle(m_hThread); }
                 
        bool Create()
        {
          m_hThread = CreateThread(0,
                                   0,
                                   ThreadFunc,
                                   this,
                                   0,
                                   &m_dwThreadID);
          if(!m_hThread)
          {
            // Could not create thread
            return false;
          }
          return true;              
        }
                  
      private:
        DWORD  m_dwThreadID;
        HANDLE m_hThread;
                  
        static DWORD WINAPI ThreadFunc(LPVOID pvParam);
      };
    • 'AfxBeginThread()'

      Code:
      class CMyDialog : public CDialog
      {
      public:
        CMyDialog(CWnd* pParent = NULL)
          : CDialog(CMyDialog::IDD, pParent)
        {
          m_pThread = 0;
        }
                  
        bool Create()
        {
          m_pThread = AfxBeginThread(ThreadFunc, this);
          if(!m_pThread)
          {
            // Could not create thread
            return false;
          }
          return true;              
        }
                  
      private:
        CWinThread *m_pThread;
                  
        static UINT ThreadFunc(LPVOID pvParam);
      };



    Last edited by Andreas Masur; July 25th, 2005 at 12:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center