91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 2KB
    文件類型: .cpp
    金幣: 1
    下載: 1 次
    發(fā)布日期: 2021-05-16
  • 語言: C/C++
  • 標簽: linux??進程??線程??

資源簡介

理發(fā)師問題的描述:一個理發(fā)店接待室有n張椅子,工作室有1張椅子;沒有顧客時,理發(fā)師睡覺;第一個顧客來到時,必須將理發(fā)師喚醒;顧客來時如果還有空座的話,他就坐在一個座位上等待;如果顧客來時沒有空座位了,他就離開,不理發(fā)了;當(dāng)理發(fā)師處理完所有顧客,而又沒有新顧客來時,他又開始睡覺。

資源截圖

代碼片段和文件信息

#include?
#include?
#include?
#include?
#include
#include
using?namespace?std;
static?int?seatNumber=30;
int?seatChair=0;?//waiting?customs
int?flag=0;
queue?guestQueue;
int?mutex=1;
void?down(int?*x)
{
????while(true)
????????if(*x>0)
????????{
????????????*x=0;
????????????break;
????????}
}
void?up(int?*x)
{
????*x=*x+1;
}
void?*thread(void?*ptr)
{
????while(true)
????{
????????flag++;
????????down(&mutex);
????????if(seatNumber>0)
????????{
????????????seatNumber--;
????????????guestQueue.push(flag);
????????????printf(“gest?comming:?%d-----seatNumber:%d?\n“flagseatNumber);
????????}
????????else
????????{
????????????printf(“gest?gone-----%d-----seatNumber:%d?\n“flagseatNumber);
????????}
????????up(&mutex);
????????if(guestQueue.size()>20)sleep(1);
????}
}
int?main(void)
{
?????pthread_t?id;
????int?ret;
????ret=pthread_create(&idNULLthreadNULL);
????if(ret!=0){
????????printf?(“Create?pthread?error!\n“);
????????exit?(1);
????}else{
????????printf?(“Create?pthread?success!\n“);

????????while(true)
????????{
????????????if(guestQueue.size()<10)sleep(1);
????????????down(&mutex);
????????????int?guestFlag

評論

共有 條評論