97.2k views
5 votes
Two agents (threads) sell tickets to a concert. Each thread will loop selling tickets until there are no more tickets left to sell. Clearly total number of tickets that can be sold (numtickets) is a global variable. Note: Write the pseudo code for one thread only. The other one should have the same code except the thread name so no need to repeat it.

Write a pseudo code to guarantee that the total number of tickets sold do not exceed numtickets

1 Answer

0 votes

Final answer:

Pseudo code to prevent total ticket sales from exceeding numtickets using a mutex lock.

Step-by-step explanation:

In order to ensure that the total number of tickets sold does not exceed the value of the global variable 'numtickets', we can use a mutex lock to synchronize access to the variable.

  1. Initialize a mutex lock.
  2. Lock the mutex before attempting to sell a ticket.
  3. Check if there are still tickets available.
  4. If there are tickets available, sell a ticket and increment the count of tickets sold.
  5. Unlock the mutex.
  6. Repeat the steps 2-5 until there are no more tickets available.

This pseudo code ensures that only one thread can access the 'numtickets' variable at a time, preventing the total number of tickets sold from exceeding the limit.

User Rebekah
by
8.8k points