C++ 我如何在不使用信号灯的情况下解决寿司店问题? 问题:

C++ 我如何在不使用信号灯的情况下解决寿司店问题? 问题:,c++,c,synchronization,semaphore,condition-variable,C++,C,Synchronization,Semaphore,Condition Variable,此代码用于同步问题,称为寿司吧问题。规则如下: 想象一下有5个座位的寿司吧。如果你在有航班的时候到达 空座位,你可以马上坐下。但是,如果你在所有座位都到的时候 吃饱了,这意味着他们都在一起吃饭,你会 在你坐下之前,你必须等待整个队伍离开 脚本: 这里的代码使用信号量在C中工作。我一直试图在没有信号灯的情况下写它,但是没有用。它不一定是C,它可以是C++或其他语言。 我在考虑条件变量,但我不确定如何实现它们。如果有人能帮忙,我会非常感激的 #include <stdio.h> #inc

此代码用于同步问题,称为寿司吧问题。规则如下:

想象一下有5个座位的寿司吧。如果你在有航班的时候到达 空座位,你可以马上坐下。但是,如果你在所有座位都到的时候 吃饱了,这意味着他们都在一起吃饭,你会 在你坐下之前,你必须等待整个队伍离开

脚本: 这里的代码使用信号量在C中工作。我一直试图在没有信号灯的情况下写它,但是没有用。它不一定是C,它可以是C++或其他语言。 我在考虑条件变量,但我不确定如何实现它们。如果有人能帮忙,我会非常感激的

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdbool.h>

int eating = 0, waiting = 0;
bool must_wait = false;
sem_t block;                                                            
sem_t mutex;

void * sushiBar(void * threadID)
{
usleep(rand() % 1000);

sem_wait(&mutex);
if(must_wait){
    printf("Waits: %d seats are available. %d other people waiting.\n", 5-eating, waiting);
    waiting++;
    sem_post(&mutex);
    sem_wait(&block);
    waiting--;
}
if(eating == 4)
    printf("Last seat is taken.\n");
else{
    printf("%d seats available. Sits and eats sushi.\n", 5-eating);
}

eating++;
must_wait = (eating == 5);

if(waiting && (!must_wait))
    sem_post(&block);
else
    sem_post(&mutex);

usleep((rand() % 901) + 100);
sem_wait(&mutex);
eating--;
printf("Customer leaves: %d seats are available.\n", 5-eating);
if (eating == 0)
    must_wait = false;
if ( waiting && (!must_wait))
    sem_post(&block);
else
    sem_post(&mutex);
return 0;
}

int main(){
 int n=10,i=0,retVal=0;
 pthread_t *thread;

sem_init(&mutex, 0, 1);
sem_init(&block, 0, 0);

thread = (pthread_t *) malloc (n*sizeof(pthread_t));

for (i=0; i<n; i++){
  retVal = pthread_create(&thread[i], NULL, sushiBar, (void*)&i);
  if (retVal != 0){
    exit(EXIT_FAILURE);        
   }
}

for(i=0; i<n; i++){
    retVal = pthread_join(thread[i],NULL);
        if(retVal != 0){
           exit(EXIT_FAILURE);        
        }
 }

return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int吃=0,等待=0;
bool must_wait=false;
sem_t块;
sem_t互斥体;
void*sushisbar(void*threadID)
{
usleep(兰特()%1000);
sem_等待(&mutex);
如果(必须等待){
printf(“等待:%d个座位可用。%d个其他人等待。\n”,5-eating,等待);
等待++;
sem_post(和互斥);
sem_等待(&block);
等待--;
}
如果(吃=4)
printf(“最后一个座位有人坐了。\n”);
否则{
printf(“%d个座位可用。坐着吃寿司。\n”,5-eating);
}
饮食++;
必须等待=(进食==5);
如果(等待&(!必须等待))
sem_柱和砌块;
其他的
sem_post(和互斥);
usleep((rand()%901)+100);
sem_等待(&mutex);
吃——;
printf(“客户假期:%d个座位可用。\n”,5-3);
如果(吃=0)
必须等待=错误;
如果(等待&(!必须等待))
sem_柱和砌块;
其他的
sem_post(和互斥);
返回0;
}
int main(){
int n=10,i=0,retVal=0;
pthread_t*线程;
sem_init(&mutex,0,1);
sem_init(&block,0,0);
线程=(pthread_t*)malloc(n*sizeof(pthread_t));

对于(i=0;i,伪代码如下所示:

// global varaible
int seatsAtSushiBar = 5;

void
routineAtSushiBar()
{
    grabSeatAtSushiBar();
    haveLunch();
    releaseSeatAtSushiBar();
}

void
grabSeatAtSushiBar()
{
    // Get exclusive lock.
    condition_mutex->lock();
        while (0 == seatsAtSushiBar) {
            // Sleep while no seat available.
            condition_mutex->wait();
            // After wakeup you must check the conditon again, 
            // to protect from spurious wakeup calls.
        }
        // Seat is available grab one for yourself.
        seatsAtSushiBar--;
    condition_mutex->unlock();
}

void
releaseSeatAtSushiBar()
{
    // Get exclusive lock.
    condition_mutex->lock();
        // Release the seat.
        seatsAtSushiBar++;
        if (1 == seatsAtSushiBar) {
            // Wakeup a blocking customer to grab the seat.
            condition_mutex->notify();
        }
    condition_mutex->unlock();
}

我已经用C++中的condition_变量(基本实现)实现了这个问题。您可以使用它作为起点并尝试开发您的应用程序

#include <iostream>           
#include <thread>             
#include <mutex>           
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
int noofseats = 0;
bool ready = false;

void enter_bar(int id) {
    std::unique_lock<std::mutex> lck(mtx);

    while (noofseats  >= 5)
    {
        //lock threads if seats are filled
        cv.wait(lck);
        std::cout << "User :  " << id << "waiting for seat" << std::endl;

    }
    std::cout << "User :  " << id << "got seat" << std::endl;
    noofseats++;

}

void exit_bar()
{
    std::unique_lock<std::mutex> lck(mtx);
    noofseats--;
    if(noofseats < 5)
    {
        //would unloack other threads if seats are present
        cv.notify_all();
    }

}

int main()
{
    std::thread threads[10];
    // spawn 10 threads:
    for (int i = 0; i<10; ++i)
    {
        threads[i] = std::thread(enter_bar, i);
    }

    //three people exit bar
    exit_bar();       
    exit_bar();
    exit_bar();

    for (auto& th : threads) th.join();

    return 0;
}
#包括
#包括
#包括
#包括
std::互斥mtx;
std::条件变量cv;
int noofseat=0;
bool ready=false;
无效输入栏(内部id){
标准:唯一锁定lck(mtx);
while(noofseat>=5)
{
//如果阀座已填满,则锁紧螺纹
cv.等待(lck);

std::如果所有五个座位都坐满了,那么一个人应该等到所有人都离开。我该如何实现呢?还有,在你主要调用exit_-bar()的地方,为什么会有三个人离开?如果一个人进入酒吧,则执行例程enter_-bar(),并执行变量noofseat(占用的座位数)当该值等于5时,条件变量被锁定,当peson退出bar时,person必须等待Exit_bar被执行,当小于5时,noofsetas被删除。在上面的程序中,条件变量被解锁。在上面的程序中,我刚刚考虑到10人进入bar,其中3人将退出意味着当bar退出时客满(有5人在里面)而有人来到酒吧想要进去,那人必须等到所有5人都离开酒吧。否则,如果有人来到酒吧,但酒吧还没有客满(例如4人),他们可以进入。然后下一个人必须等到所有人离开,因为酒吧已经满了。我该如何实现这一点?我真的很感谢你的帮助,但我仍然没有得到它。