Question: Project 1 Designing a thread poolThread pools were introduced in Section Thread pools. When thread pools are used, a task is submitted to the pool

Project 1Designing a thread poolThread pools were introduced in Section Thread pools. 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.This project involves creating and managing a thread pool, and it may be completed using either Pthreads and POSIX synchronization or Java. Below we provide the details relevant to each specific technology.I. POSIXThe POSIX version of this project will involve creating a number of threads using the Pthreads API as well as using POSIX mutex locks and semaphores for synchronization.The clientUsers of the thread pool will utilize the following API:void pool_init()Initializes the thread pool.int pool_submit(void (*somefunction)(void *p), void *p)where somefunction is a pointer to the function that will be executed by a thread from the pool and p is a parameter passed to the function.void pool_shutdown(void)Shuts down the thread pool once all tasks have completed.We provide an example program client.c in the source code download that illustrates how to use the thread pool using these functions.Implementation of the thread poolIn the source code download we provide the C source file threadpool.c as a partial implementation of the thread pool. You will need to implement the functions that are called by client users, as well as several additional functions that support the internals of the thread pool. Implementation will involve the following activities:The pool_init() function will create the threads at startup as well as initialize mutual-exclusion locks and semaphores.The pool_submit() function is partially implemented and currently places the function to be executedas well as its data into a task struct. The task struct represents work that will be completed by a thread in the pool. pool_submit() will add these tasks to the queue by invoking the enqueue() function, and worker threads will call dequeue() to retrieve work from the queue. The queue may be implemented statically (using arrays) or dynamically (using a linked list).The pool_init() function has an int return value that is used to indicate if the task was successfully submitted to the pool (0 indicates success, 1 indicates failure). If the queue is implemented using arrays, pool_init() will return 1 if there is an attempt to submit work and the queue is full. If the queue is implemented as a linked list, pool_init() should always return 0 unless a memory allocation error occurs.The worker() function is executed by each thread in the pool, where each thread will wait for available work. Once work becomes available, the thread will remove it from the queue and invoke execute() to run the specified function.A semaphore can be used for notifying a waiting thread when work is submitted to the thread pool. Either named or unnamed semaphores may be used. Refer to Section POSIX semaphores for further details on using POSIX semaphores.A mutex lock is necessary to avoid race conditions when accessing or modifying the queue. (Section POSIX mutex locks provides details on Pthreads mutex locks.)The pool_shutdown() function will cancel each worker thread and then wait for each thread to terminate by calling pthread_join(). Refer to Section Thread cancellation for details on POSIX thread cancellation. (The semaphore operation sem_wait() is a cancellation point that allows a thread waiting on a semaphore to be cancelled.)Refer to the source-code download for additional details on this project. In particular, the README file describes the source and header files, as well as the Makefile for building the project.II. JavaThe Java version of this project may be completed using Java synchronization tools as described in Section 7.4. Synchronization may depend on either (a) monitors using synchronized/wait()/notify()(Section Java monitors) or (b) semaphores and reentrant locks (Section Reentrant locks and Section Semaphores). Java threads are described in Section Java threads.Implementation of the thread poolYour thread pool will implement the following API:ThreadPool()Create a default-sized thread pool.ThreadPool(int size)Create a thread pool of size size.void add(Runnable task)Add a task to be performed by a thread in the pool.void shutdown()Stop all threads in the pool.We provide the Java source file ThreadPool.java as a partial implementation of the thread pool in the source code download. You will need to implement the methods that are called by client users, as well as several additional methods that support the internals of the thread pool. Implementation will involve the following activities:The constructor will first create a number of idle threads that await work.Work will be submitted to the pool via the add() method, which adds a task implementing the Runnable interface. The add() method will place the Runnable task into a queue (you may use an available structure from the Java API, such as java.util.List).Once a thread in the pool becomes available for work, it will check the queue for any Runnable tasks. If there is such a task, the idle thread will remove the task from the queue and invoke its run() method. If the queue is empty, the idle thread will wait to be notified when work becomes available. (The add() method may implement notification using either notify() or semaphore operations when it places a Runnable task into the queue to possibly awaken an idle thread awaiting work.)The shutdown() method will stop all threads in the pool by invoking their interrupt() method. This, of course, requires that Runnable tasks being executed by the thread pool check their interruption status (Section Thread cancellation).Refer to the source-code download for additional details on this project. In particular, the README file describes the Java source files, as well as further details on Java thread interruption.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!