#include<stdio.h>
#include<windows.h>
volatile long g_nLoginCount;
unsigned int _stdcall Fun(void *pPM);
const DWORD THREAD_NUM = 50;
DWORD WINAPI ThreadFun(void *pPM)
{
Sleep(10);
InterlockedIncrement((PLONG)&g_nLoginCount);
Sleep(10);
return 0;
}
int main()
{
int num=20;
while(num--)
{
g_nLoginCount=0;
int i;
HANDLE handle[THREAD_NUM];
for(i=0;i<THREAD_NUM;i++)
handle[i]=CreateThread(NULL,0,ThreadFun,NULL,0,NULL);
WaitForMultipleObjects(THREAD_NUM,handle,true,INFINITE);
printf("有%d个用户登录,纪录结果是:%d\n",THREAD_NUM,g_nLoginCount);
}
system("pause");
return 1;
}
转发地址:http://blog.csdn.net/morewindows/article/details/7429155