Reputation: 121
I was given a question some time ago that I am not sure of how to answer, the question is typed below: can anyone give me an idea of how i should go about doing this using C++
write code to help synchronize a professor and his/her students during office hours. The professor, of course, wants to take a nap if no students are around to ask questions; if there are students who want to ask questions, they must synchronize with each other and with the professor so that only one person is speaking at any one time, each student question is answered by the professor, and no student asks another question before the professor is done answering the previous one. You are to write four procedures: AnswerStart(), AnswerDone(), QuestionStart() , and QuestionDone(). The professor loops running the code: AnswerStart(); give answer; AnswerDone(). AnswerStart doesn't return until a question has been asked. Each student loops running the code: QuestionStart(); ask question; QuestionDone(). QuestionStart() does not return until it is the student's turn to ask a question. Since professors consider it rude for a student not to wait for an answer, QuestionEnd() should not return until the professor has finished answering the question.
Upvotes: 0
Views: 612
Reputation: 2703
you are going to have to have serialized access to variables and use locks to make sure multiple actions are not happening at once. The first part of this document explains how to use locks in c++. Hopefully that will point you in the right direction.
http://www.cs.utexas.edu/~lavender/courses/cs371/lectures/lecture-10.pdf
Upvotes: 1
Reputation: 1278
I am assuming this is a class assignment so I won't give away the answer.
I would handle this with an active object.
Upvotes: 1
Reputation: 3131
Readers/Writer locks might be what you are looking for.
This sounds like a homework question, as per the community guidelines can you let us know what you've tried?
Upvotes: 1