// testMutiThread.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <WinBase.h>
#include <stdio.h>
#include <process.h>
#pragma comment(lib,"libcmt.lib")
const int NLOOP = 100;
int counter = 0;
void doit(void*);
CRITICAL_SECTION beswap ;
int main()
{
HANDLE pnt[2];
InitializeCriticalSection(&beswap);
pnt[0] = (HANDLE)_beginthread(doit,0,NULL);
pnt[1] = (HANDLE)_beginthread(doit,0,NULL);
WaitForMultipleObjects( 2, pnt, TRUE, 1000L);
DeleteCriticalSection(&beswap);
return 0;
}
void doit(void*)
{
printf("go.../n");
int i, val = 0;
for(i = 0; i < NLOOP; ++i)
{
EnterCriticalSection(&beswap);
val = counter;
printf("%d/n",val+1);
counter = val + 1;
LeaveCriticalSection(&beswap);
}
printf("end.../n");
return ;
}
windows 多线程示例
最新推荐文章于 2024-09-09 20:17:28 发布