VC++MSDN 의beginthreadex 와endthreadex 의 사용 예

전환 하 다
http://blog.csdn.net/pacewalker/article/details/6947123
1._beginthread, _beginthreadex .
스 레 드 만 들 기

uintptr_t _beginthread( 
   void( *start_address )( void * ),
   unsigned stack_size,
   void *arglist 
);
uintptr_t _beginthreadex( //    
   void *security, //    ,NULL       
   unsigned stack_size, //         。   0,                 。   0
   unsigned ( *start_address )( void * ), //         ,              (       ,         )
   void *arglist, //         ;            //           ,                
   unsigned initflag, //      ,0:    ;CREATE_SUSPEND:suspended(  )
   unsigned *thrdaddr //      ID   
);

인자:
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.
대beginthread,호출 약속 은cdecl 또는clrcall; 대beginthreadex,eitherstdcall 또는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.
2._endthread, _endthreadex
각 사용 종료beginthread or _beginthreadex 가 만 든 스 레 드


void _endthread( void );
void _endthreadex( //    
   unsigned retval 
);

예:


// crt_begthrdex.cpp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>


unsigned Counter; 
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
    printf( "In second thread...
" ); while ( Counter < 1000000 ) Counter++; _endthreadex( 0 ); return 0; } int main() { HANDLE hThread; unsigned threadID; printf( "Creating second thread...
" ); // Create the second thread. hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // Wait until second thread terminates. If you comment out the line // below, Counter will not be correct because the thread has not // terminated, and Counter most likely has not been incremented to // 1000000 yet. WaitForSingleObject( hThread, INFINITE ); printf( "Counter should be 1000000; it is-> %d
", Counter ); // Destroy the thread object. CloseHandle( hThread ); }

출력:


Creating second thread...
In second thread...
Counter should be 1000000; it is-> 1000000

비교적 구체 적 인 예 들:
http://hi.baidu.com/qinpc/blog/item/5aaa8b54918e541b3a2935ee.html
http://www.cppblog.com/mzty/archive/2007/07/25/28756.html

좋은 웹페이지 즐겨찾기