pthread's programming problem
the problem is this:
AirCanada has 100 seats available for flight888, and there are 10 travel agents that's selling these 100 seats. So I am going to use 10 threads to indicate the 10 travel agents. Whichever a thread book a seat, the seats_available is decrement by 1. Basically the seats_available is a global variable. The ratio of booking/cancelling is 3:1, so every 3 seats booking will result a seat of cancelling.
I have to avoid the any two or more threads updating the seats_available at the same time. So what should I do? how do I code it in c or c++ that will act in a way so every 3 seats booked and 1 seat will be canceled? do I need to use random seed for the 10 threads to decide which one is going to perform some kind of action.
The program will end when seats_available is 0.
and constructive idea is welcomed.
|