English 中文(简体)
洗浴室同化(复制)
原标题:Trouble figuing out logic for bathroom syncronization [duplicate]
This question already has an answer here:
Closed 11 years ago.

Possible Duplicate:
bathroom synchronization and queue of threads

我在 st中审视了类似的问题,并用了眼光来回答我的问题。 就家庭工作而言,我得到了浴室同步问题。 一个共用的浴室,一个妇女不能使用,而一个男子在那里,反之亦然。 我试图说明的是,如果另一性别在浴室里的话,如何阻止和重新铺面。 到目前为止,我有几种条件,如果不存在异性,如果我不告诉胎儿等待。 离开时,如果你的性别没有在休息室里留下,那么(semaphore)的另一种性就让了。 我不知道,如果我遇到什么麻烦,要打上read子,或让ema人等待。 这里是我的法典。

/ 变数

 sem_t male;
 sem_t female;
 int maleInBath;
 int femaleInBath;
 pthread_mutex_t coutMutex;

/ 初始变量

void personInitGlobals()
{
   // LEAVE THIS STATEMENT                                                 
   pthread_mutex_init(&coutMutex, NULL);

   // TODO: Complete this function                                         
   int init=0;
   maleInBath=0;
   femaleInBath=0;
   sem_init(&male, 0, init);
   sem_init(&female, 0, init);
}

休息室

void personEnterRestroom(int id, bool isFemale)
 {
   // LEAVE THESE STATEMENTS                                               
    pthread_mutex_lock(&coutMutex);
    cout << "Enter: " << id << (isFemale ? " (female)" : " (male)") << endl;
    pthread_mutex_unlock(&coutMutex);

   // TODO: Complete this function                                         
   if(isFemale && maleInBath==0){
     femaleInBath++;
    }else if(isFemale && maleInBath >0){
      sem_wait(&female);
   }else if(!isFemale && femaleInBath==0){
      maleInBath++;
  }else{
    sem_wait(&male);
  }
}

离开休息室

void personLeaveRestroom(int id, bool isFemale)
 {
   // LEAVE THESE STATEMENTS                                               
    pthread_mutex_lock(&coutMutex);
    cout << "Leave: " << id << (isFemale ? " (female)" : " (male)") << endl;
    pthread_mutex_unlock(&coutMutex);

   // TODO: Complete this function                                         
   if(isFemale){
     femaleInBath--;
    if(femaleInBath==0){
      sem_post(&male);

    }
   }else{
     maleInBath--;
     if(maleInBath==0){
     sem_post(&female);

  }
}

}

问题回答

Well as awoodland already have linked to the other thread on SO, so its good to have a look on it. Just in case to avoid duplicated solutions to same problem in same class, I will also ask you whether use of semaphore is mandatory??

If not, then just create a queue (first in first go) structure and put every incomming person to queue. When new person comes, apply your bathroom availability logic. Also when a person leaves the bathroom, apply the bathroom logic again if the queue is not empty. And thats it for you.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签