CS110 Homework 6: ThreadPool

Overview

In this homework, you will implement a naive thread pool. A thread pool is a very common design, aims to reuse threads to avoid the overhead of creating and destroying threads.

Getting started

Download the starter code here.

Ringbuffer

Before start working with pthread, we must first learn a data structure named ringbuffer.

A ringbuffer is a circular queue with a fixed maximum size. See ringbuffer.h

ringbuffer_pic_wikipedia

Threadpool

Now we are ready to implement the threadpool. See threadpool.h

We harness ringbuffer to store tasks added with threadpool_add_task.

Instead of directly initiate task functions, which cause thread creation overhead for every task, we run a worker function (static void *threadpool_thread(void *threadpool);) as a thread, it remains idle until destroyed by signals or fetch a new task.

Submission

Simply run make submit, and submit the tarball named hw6_threadpool.tar generated to autolab.

Guidance

Manual you should read (stop it by clicking on the pic)

ringbuffer_pic_wikipedia

They might be a little intimidating, but rather helpful for debugging!

How much code will I need to write?

One possible solution:

ringbuffer.c | 18 ++++--------------
threadpool.c | 42 +++++++++++++-----------------------------
2 files changed, 17 insertions(+), 43 deletions(-)

Note that there are lots of TODOs in the code, which is the cause of most of the deletion.
You can generate your own statistics by running diff -uN template answer | diffstat, where template and answer are the directories of template given and your answer respectively.