Three ways for VC to start a new thread

The first AfxBeginThread()

Use the AfxBeginThread() function to create a new thread to execute tasks. The prototype of the worker thread's AfxBeginThread is as follows:
CWinThread* AfxBeginThread(AFX_THREADPROC pfnThreadProc,
  LPVOID lParam,
  int nPriority = THREAD_PRIORITY_NORMAL,
  UINT nStackSize = 0,
  DWORD dwCreateFlags = 0,
  LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
  );//Used to create a worker thread
Return value: Returns a pointer to the thread object of the new thread on success, otherwise NULL.
pfnThreadProc : The entry function of the thread, the declaration must be as follows: UINT MyThreadFunction(LPVOID pParam), cannot be set to NULL;
pParam : The parameter passed into the thread, note that its type is: LPVOID, so we can pass a structure into the thread.
nPriority : The priority of the thread, generally set to 0. It has the same priority as the main thread.
nStackSize : Specifies the size of the stack of the newly created thread. If it is 0, the newly created thread has the same size as the main thread The stack of
dwCreateFlags : Specifies what flags the thread has after the thread is created. Two values ​​can be specified:
CREATE_SUSPENDED : After the thread is created, it will be in a suspended state until it is called: ResumeThread
0 : It will start running after the thread is created.
lpSecurityAttrs : Point to a SECURITY_ATTRIBUTES structure, which is used to mark the security of the newly created thread. If it is NULL,
the newly created thread has the same security as the main thread.
If you want to end the thread in the thread, you can Inside call AfxEndThread.

Generally use AfxBeginThread(ThreadProc, this) directly;

Example:

[cpp]  view plain copy  
  1. UINT  myproc(LPVOID  lParam)  
  2. {  
  3. CITTDlg *pWnd = (CITTDlg *)lParam;          //Assign the window pointer to an untyped pointer  
  4. pWnd->KMeansSegment();                          //function to execute  
  5. return 1;  
  6. }  
  7.   
  8. void CITTDlg::KMeansSegment()  
  9. {  
  10. // The main processing function is written here  
  11. }  
  12.   
  13. void  CITTDlg::OnKMeansSegment()              //Button click to execute  
  14. {  
  15.   
  16. AfxBeginThread(myproc, ( LPVOID ) this ); //Start a new thread  
  17.   
  18. }  

Note that the function of the worker thread must be a global function or a static member function, not a normal member function.


The second CreateThread()

The function prototype is: HANDLE CreateThread(
                           NULL, // no security descriptor
                           0, // default thread stack size
                           MyThreadProc, // thread function pointer, that is, function name
                           (LPVOID)&n, // pass parameter
                           NULL, // no additional property
                           NULL // no need to get thread number
                           );

CreatThread, which returns a handle; if you no longer need to monitor the thread, use CloseHandle() to close the thread handle.
The thread's function must be defined as: DWORD WINAPI MyThreadProc(LPVOID pParameter);

The following demonstrates the multi-threaded operation control, click a Button and then run a thread to display the string in the CEdit control;
Example:

[cpp]  view plain copy  
  1. .h header file  
  2.   
  3. struct hS  
  4.     {  
  5.         CString Tmp;  
  6.         CTestDlg *hWnd;  
  7.     }; //Define a global structure to pass custom messages  
  8.   
  9. DWORD  WINAPI ThreadProc(LPVOIDlpParam); //Thread function declaration, global function  
  10.   
  11. public:  
  12.     CString chtmp;  
  13.     struct hS    *hTmp;  
  14.   
  15. protected:  
  16.    HANDLE  m_hThread; //Thread handle  
  17.    CEdit m_Edit ;  
  18.   
  19. .cpp implementation file  
  20.   
  21. // thread execution function  
  22.   
  23. DWORD WINAPI   ThreadProc(LPVOID lpParam)  
  24. {  
  25. // write the handler function here  
  26.         struct hS *Tmp2;  
  27.         Tmp2 = (hS *) lpParam;  
  28. // operate:  
  29.        Tmp2->hWnd->m_Edit.SetWindowText( (LPTSTR)Tmp2->Tmp );  
  30. }  
  31.   
  32. void CTestDlg::OnBnClickedButton1()  
  33. {  
  34.     hTmp->Tmp = chtmp;  
  35.     hTmp->hWnd =  this ; //The key is to pass the this pointer in  
  36.    m_hThread =CreateThread(NULL,0,ThreadProc,hTmp,0,NULL);//创建新线程  
  37.    CloseHandle(m_hThread );  
  38. }  

用CreateThread()函数创建线程将返回一个线程句柄,通过该句柄你可以控制和操作该线程,当你不用时可以一创建该线程后就关闭该句柄,有专门的函CloseHandle()。关闭句柄不代表关闭线程,只是你不能在外部控制该线程(比如,提前结束,更改优先级等)。在线程结束后,系统将自动清理线程资源,但并不自动关闭该句柄,所以线程结束后要记得关闭该句柄。
 



第三种_beginthread()


函数原型为:intptr_t  _beginthread(
                                                              void( *start_address )( void * ),  //指向新线程调用的函数的起始地址
                                                              unsigned stack_size,                  //堆栈大小,设置0为系统默认值
                                                              void *arglist                                  //传递给线程函数的参数,没有则为NULL
                                                             );
返回值:
假如成功,函数将会返回一个新线程的句柄,用户可以像这样声明一个句柄变量存储返回值:
  HANDLE hStdOut = _beginthread( CheckKey, 0, NULL )。如果失败_beginthread将返回-1。
所在库文件:
#include <process.h>
Definition of thread function:
For the thread created by _beginthread(), its thread function is defined as:
void ThreadPro(void * pArguments );

_beginthreadex() is an upgraded version of _beginthread().


Summarize:
AfxBeginThread is a global function of MFC, which is the encapsulation of CreateThread. CreateThread is a Win32 API function, and AfxBeginThread will eventually be transferred to CreateThread. And _beginthread is a C runtime library function.


Copyright statement: https://blog.csdn.net/u014568921/article/details/44262645

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325366068&siteId=291194637