MSDN 에서 대부분 예제를 가져다가 작동하게 만들었습니다. VS.NET 이상에서 작동합니다.
VS6.0에서 성공하신 분 있으면 알려주세요.
~cpp
// crt_begthrdex.cpp
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__beginthread.2c_._beginthreadex.asp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>
unsigned Counter;
CRITICAL_SECTION cs;
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/overlapped_str.asp
unsigned __stdcall ThreadedFunction( void* pArguments )
{
printf( "In second thread...n" );
while ( Counter < 100 ){
EnterCriticalSection(&cs);
printf( "Thread ID %d => %dn", pArguments, Counter);
Counter++;
LeaveCriticalSection(&cs);
}
_endthreadex( 0 );
return 0;
}
int main()
{
HANDLE hThread, hThread2;
unsigned threadID = 1;
unsigned threadID2 = 2;
printf( "Creating second thread...n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &ThreadedFunction, NULL, 0, &threadID );
hThread2 = (HANDLE)_beginthreadex( NULL, 0, &ThreadedFunction, NULL, 0, &threadID2 );
InitializeCriticalSection(&cs);
// 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-> %dn", Counter );
system("pause");
// Destroy the thread object.
CloseHandle( hThread );
CloseHandle( hThread2 );
DeleteCriticalSection(&cs);
return 0;
}
VS.NET에서도 함수이름을 인식하지 못하는 경우 프로젝트 세팅에서 MFC라이브러리를 사용하도록 해보세요.