天天看点

PV读者写者问题

semaphore mutex = 1;
semaphore db = 1;
int readercount = 0;

void Reader()
{
	while(true)
	{
		P(mutex);
		readercount ++;
		if(readercount == 1)
			P(db);
		V(mutex);
		
		//读数据
		
		P(mutex);   //读完了要走 
		readercount --;
		if(readercount == 0)
			V(db);
		V(mutex);
	}
}

void Writer()
{
	P(db);
	
	//写数据 
	
	V(db);
}
           

继续阅读