#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);int index=0;
int tickets=20;
HANDLE hMutex;void main()
{
HANDLE hThread1;
HANDLE hThread2;hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2); hMutex=CreateMutex(NULL,FALSE,NULL);
Sleep(10000);
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{ while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<" thread1 sell ticket:"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
} DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0){
Sleep(1);
cout<<" thread2 sell ticket:"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
C++ 多线程 CreateMutex
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++ | C++多线程编程
PTHREAD_CREATE(3)NAME pthread_create -
c++ c语言 #include 多任务处理 g++ -
02 C++多线程
线程之间共享资源详情请见银行家算法此时就意味着我们需要给线程加上锁保证每个线程能够独立的,可能会忘记,
c++ 多线程 互斥量 互斥 #include -
C++多线程介绍
文章目录线程控制块常用APIC语言
c++ 多线程 #include ios Windows