This is a simple thread pool implementation that has some enhanced features such as "wait_for_task ".
You can specifiy the number of threads of the pool and submit your task to the pool. Specifically, You may refer to this page to get a full understanding.
The repo has a static library named libpool.a, you can directly add this lib to your own code. An example of usage is listed below:
- To use the ThreadPool class you create, a user would create a subclass of Task that implements the Run() method that performs an operation they want to add to the queue of operations to do:
class ComputeSumTask : public Task {
public:
ComputeSumTask(int *sum_destination, int *array_to_sum, int array_size) {
this->sum_destination = sum_destination;
this->array_to_sum = array_to_sum;
this->array_size = array_size;
}
void Run() {
int sum = 0;
for (int i = 0; i < this->array_size; ++i) {
sum += this->array_to_sum[i];
}
this->sum_destination = sum;
}
int *sum_destination,
int *array_to_sum;
int array_size;
};
- Notice that the Task subclass can (and typically would) contain member variables. Then, submit a bunch of instances of this class for each thing they wanted to do in parallel
int arrayA[ARRAY_SIZE], arrayB[ARRAY_SIZE];
int sum_of_A, sum_of_B;
ThreadPool pool(num_threads);
pool.SubmitTask("sum arrayA", new ComputeSumTask(&sum_of_A, arrayA, ARRAY_SIZE));
pool.SubmitTask("sum arrayB", new ComputeSumTask(&sum_of_B, arrayB, ARRAY_SIZE));
- Finally wait for the tasks to complete before stopping the thread pool(but this step is optional):
pool.WaitForTask("sum arrayA");
pool.WaitForTask("sum arrayB");
pool.Stop();
Some knowledge about pthread: before invoking pthread_cond_signal, make sure to have the lock held. This ensures that we don't accidentally introduce a race condition into our code.
This library is just for study. Full correctness is not guranteed.