程式扎記: [C++ 範例代碼] 在 Windows 下建立 Thread (使用 _beginthread, _beginthreadex)

標籤

2010年8月6日 星期五

[C++ 範例代碼] 在 Windows 下建立 Thread (使用 _beginthread, _beginthreadex)


前言 :
這裡介紹如何在Windows 利用函式 _beginthread, _beginthreadex下建立 Thread. (更多說明可以參考 這裡)

函式介紹 :
語法 :
  1. uintptr_t _beginthread(   
  2.    void( *start_address )( void * ),  
  3.    unsigned stack_size,  
  4.    void *arglist   
  5. );  
  6. uintptr_t _beginthreadex(   
  7.    void *security,  
  8.    unsigned stack_size,  
  9.    unsigned ( *start_address )( void * ),  
  10.    void *arglist,  
  11.    unsigned initflag,  
  12.    unsigned *thrdaddr   
  13. );  
參數 :
start_address
Start address of a routine that begins execution of a new thread. For _beginthread, the calling convention is either __cdecl or __clrcall; for_beginthreadex, it is either __stdcall or __clrcall.

stack_size
Stack size for a new thread or 0.

arglist
Argument list to be passed to a new thread or NULL.

security
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If NULL, the handle cannot be inherited. Must be NULL for Windows 95 applications.

initflag
Initial state of a new thread (0 for running or CREATE_SUSPENDED for suspended); use ResumeThread to execute the thread.

thrdaddr
Points to a 32-bit variable that receives the thread identifier. Might be NULL, in which case it is not used.

返回值 :
If successful, each of these functions returns a handle to the newly created thread; however, if the newly created thread exits too quickly,_beginthread might not return a valid handle. _beginthread returns 1L on an error, in which case errno is set to EAGAIN if there are too many threads or to EINVAL if the argument is invalid or the stack size is incorrect. _beginthreadex returns 0 on an error, in which case errno and _doserrno are set.
If startaddress is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return -1.
For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
For more information about uintptr_t, see Standard Types.

使用函式庫 :
Multithreaded versions of the C run-time libraries only.
To use _beginthread or _beginthreadex, the application must link with one of the multithreaded C run-time libraries.

使用範例 :
The following sample code demonstrates how you can use the thread handle returned by _beginthreadex with the synchronization APIWaitForSingleObject. The main thread waits for the second thread to terminate before it continues. When the second thread calls _endthreadex, it causes its thread object to go to the signaled state. This allows the primary thread to continue running. This cannot be done with _beginthread and _endthread, because _endthread calls CloseHandle, destroying the thread object before it can be set to the signaled state.
- 範例代碼 :
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. unsigned icounter=0;  
  8. unsigned icounterMax = 100000;  
  9. unsigned  __stdcall secondThreadFunc(void* argu) {  
  10.     printf("In second thread...\n");  
  11.     while(icounter < icounterMax)  
  12.         icounter++;  
  13.     _endthreadex(0);          
  14.     return 0;  
  15. }  
  16.   
  17. void main() {  
  18.     HANDLE hThread;   
  19.     printf("Create second thread...\n");  
  20.     //createThreadEx(&hThread, (void*) &secondThreadFunc, NULL, 0, 0);  
  21.     unsigned threadID;  
  22.     hThread = (HANDLE)_beginthreadex(NULL, 0, &secondThreadFunc, NULL, 0, &threadID);  
  23.     WaitForSingleObject(hThread, INFINITE);  
  24.     printf("Counter should be %d, it is %d now!\n", icounterMax, icounter);  
  25.     CloseHandle(hThread);  
  26. }  

執行結果 :
Create second thread... # 在 Main Thread
In second thread... # 在Sub Thread
Counter should be 100000, it is 100000 now! #在MainThread

補充說明 :
C++ Multithread 簡單範例
This message was edited 4 times. Last update was at 06/08/2010 16:39:22

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!