To demonstrate the use of multi-threading in programs, here is a problem:
Customers enter a bank and need to be served by one of three bankers. Each banker has a queue of customers who they need to serve.
A program that does not make use of threads would not be able to make use of all the bankers at the same time. However, a program that makes use of threads would implement at least 1 thread per banker, and therefore allowing the bankers to operate simultaneously.
To solve the problem, I have created a program that starts by asking the user to insert the amount of customers to be generated. The customers could take a random time to generate, but for testing purposes the time taken was set to a static number of 800ms.
When a customer is generated, it is added to the queue with the least
amount of customers. Each banker takes 200ms – 400ms to tend to each customer, and will tend to the next customer in its queue immediately after.
At the start of the program, the user can also enter the amount of threads to be used to generate the customers. The program itself creates one thread for each banker – meaning depending on the machine being used, increasing the amount of threads for the customer generation could have little to no impact. Using an 8 core machine, the increase in performance plateaued at around 5-6 threads as expected.
Here is a gif of the program running using 40 customers and 6 threads:
How it works:
To safely generate customers using multithreading, a channel class is used. The channel has a read and write function which both use a number of mutexes and a semaphore to operate safely.
The channel will ‘write’ a customer to a specific queue after waiting the static amount of time it takes to generate a customer.
The channel will ‘read’ the data when a customer is sent to the next free banker.
The semaphore inside the channel class signals when a customer has been ‘written’, and will ‘wait’ when the banker is free.
A mutex is also used for the print function to lock the cout function. This will avoid multiple threads trying to print at the same time.
The program decreases in performance when the number of threads is increased to a significant number such as 10,000 – creating a huge overhead of threads being created while the program is running.