|
#include<stdio.h>
#include<windows.h>
int a=0;
CRITICAL_SECTION gcs;
DWORD WINAPI RecvThread(LPVOID lp)
{
int num=*(DWORD*)lp;
while(a<100000)
{
printf("Thread %d a= %d\n",num,a);
EnterCriticalSection(&gcs);
a++;
LeaveCriticalSection(&gcs);
}
return 0;
}
void main(void)
{
InitializeCriticalSection(&gcs);
HANDLE Thread[10];
for(int i=0;i<10;i++)
{
Thread=CreateThread( NULL,0,RecvThread,&i, 0,NULL);
Sleep(500);
}
WaitForMultipleObjects(10,Thread,TRUE,100000);
DeleteCriticalSection(&gcs);
CloseHandle(Thread);
return;
} |
|