要求:A线程和B线程同时工作,A、B线程打印一串字符串休息5秒后再次打印该字符串,在A线程打印的过程中,B线程不能打断A线程的打印信息
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
HANDLE hMutex ;
DWORD WINAPI thread_A(LPVOID lpParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i=0;i<2;i++)
{
printf("AAAAA\n");
Sleep(800);
}
ReleaseMutex(hMutex);
return 0;
}
DWORD WINAPI thread_B(LPVOID lpParam)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i=0;i<2;i++)
{
printf("BBBBB\n");
Sleep(800);
}
ReleaseMutex(hMutex);
return 0;
}
int main()
{
hMutex = CreateMutex(NULL,false,NULL);
HANDLE buf[2];
buf[0] = CreateThread(NULL,0,thread_A,NULL,0,NULL);
buf[1] = CreateThread(NULL,0,thread_B,NULL,0,NULL);
WaitForMultipleObjects(2,buf,TRUE,INFINITE);
CloseHandle(hMutex);
system("pause");
return 0;
}