blob: 57ed3d4db60eb827da27751b866b4eaffe3de4cf [file] [log] [blame] [view]
Marat Dukhan90a03e72015-08-25 13:35:31 -04001# pthreadpool
Marat Dukhan712fcb32017-03-06 14:35:05 -05002
3[![BSD (2 clause) License](https://img.shields.io/badge/License-BSD%202--Clause%20%22Simplified%22%20License-blue.svg)](https://github.com/Maratyszcza/pthreadpool/blob/master/LICENSE)
4[![Build Status](https://img.shields.io/travis/Maratyszcza/pthreadpool.svg)](https://travis-ci.org/Maratyszcza/pthreadpool)
5
Marat Dukhan88042a02019-10-19 05:41:08 -04006**pthreadpool** is a portable and efficient thread pool implementation.
7It provides similar functionality to `#pragma omp parallel for`, but with additional features.
Marat Dukhan90a03e72015-08-25 13:35:31 -04008
9## Features:
10
11* C interface (C++-compatible).
Marat Dukhan88042a02019-10-19 05:41:08 -040012* 1D-6D loops with step parameters.
Marat Dukhan90a03e72015-08-25 13:35:31 -040013* Run on user-specified or auto-detected number of threads.
14* Work-stealing scheduling for efficient work balancing.
Marat Dukhan88042a02019-10-19 05:41:08 -040015* Wait-free synchronization of work items.
Marat Dukhanbe1bd8e2020-04-07 19:14:45 -070016* Compatible with Linux (including Android), macOS, iOS, Windows, Emscripten environments.
Marat Dukhan88042a02019-10-19 05:41:08 -040017* 100% unit tests coverage.
18* Throughput and latency microbenchmarks.
Marat Dukhan90a03e72015-08-25 13:35:31 -040019
20## Example
21
22 The following example demonstates using the thread pool for parallel addition of two arrays:
23
Marat Dukhanab8e1022016-03-05 19:02:13 -050024```c
25static void add_arrays(struct array_addition_context* context, size_t i) {
26 context->sum[i] = context->augend[i] + context->addend[i];
27}
Marat Dukhan90a03e72015-08-25 13:35:31 -040028
Marat Dukhanab8e1022016-03-05 19:02:13 -050029#define ARRAY_SIZE 4
Marat Dukhan90a03e72015-08-25 13:35:31 -040030
Marat Dukhanab8e1022016-03-05 19:02:13 -050031int main() {
32 double augend[ARRAY_SIZE] = { 1.0, 2.0, 4.0, -5.0 };
33 double addend[ARRAY_SIZE] = { 0.25, -1.75, 0.0, 0.5 };
34 double sum[ARRAY_SIZE];
Marat Dukhan90a03e72015-08-25 13:35:31 -040035
Marat Dukhanab8e1022016-03-05 19:02:13 -050036 pthreadpool_t threadpool = pthreadpool_create(0);
37 assert(threadpool != NULL);
Marat Dukhan2e59d6f2020-03-23 05:44:19 -070038
Marat Dukhanab8e1022016-03-05 19:02:13 -050039 const size_t threads_count = pthreadpool_get_threads_count(threadpool);
40 printf("Created thread pool with %zu threads\n", threads_count);
Marat Dukhan90a03e72015-08-25 13:35:31 -040041
Marat Dukhanab8e1022016-03-05 19:02:13 -050042 struct array_addition_context context = { augend, addend, sum };
Marat Dukhan88042a02019-10-19 05:41:08 -040043 pthreadpool_parallelize_1d(threadpool,
44 (pthreadpool_task_1d_t) add_arrays,
Marat Dukhan2e59d6f2020-03-23 05:44:19 -070045 (void*) &context,
Marat Dukhan88042a02019-10-19 05:41:08 -040046 ARRAY_SIZE,
47 PTHREADPOOL_FLAG_DISABLE_DENORMALS /* flags */);
Marat Dukhan2e59d6f2020-03-23 05:44:19 -070048
Marat Dukhanab8e1022016-03-05 19:02:13 -050049 pthreadpool_destroy(threadpool);
50 threadpool = NULL;
Marat Dukhan90a03e72015-08-25 13:35:31 -040051
Marat Dukhanab8e1022016-03-05 19:02:13 -050052 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Augend",
53 augend[0], augend[1], augend[2], augend[3]);
54 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Addend",
55 addend[0], addend[1], addend[2], addend[3]);
56 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Sum",
57 sum[0], sum[1], sum[2], sum[3]);
Marat Dukhan90a03e72015-08-25 13:35:31 -040058
Marat Dukhanab8e1022016-03-05 19:02:13 -050059 return 0;
60}
61```