Question
Project: Designing a Thread Pool. Project Description: A Thread Pool is a famous design pattern which consists of a number N of threads running a
Project: Designing a Thread Pool.
Project Description: A Thread Pool is a famous design pattern which consists of a number N of threads running a number M of jobs concurrently. The main motivation behind using the pattern is to avoid the overhead associated with creating and destroying threads. The more straight-forward approach of handling M independent jobs would be to spawn a thread for each job and destroy the thread once the job is executed. However, in some use-cases, such as when the number M of jobs is big, and the jobs are relatively small, too many threads will be generated. In modern operating systems creating a thread or a process is an expensive operation, so avoiding it in those use-cases will improve the performance of the system as well as guarantee that the system won't spawn too many processes. Instead of creating a thread for each task, thread pool normally creates threads one time at the beginning and maintains the threads in running/sleeping condition throughout its lifetime. Tasks are passed to the worker threads one by one only when a worker thread is finished with the previous task and can accept a task.
When thread pools are used, a task is submitted to the pool and executed by a thread from the pool. Work is submitted to the pool using a queue, and an available thread removes work from the queue. If there are no available threads, the work remains queued until one becomes available. If there is no work, threads await notification until a task becomes available.
Now your task is to design a thread pool with synchronization between threads.
Note: Code should be in C language and explain the code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started