1# pthreadpool 2 3[](https://github.com/Maratyszcza/pthreadpool/blob/master/LICENSE) 4[](https://travis-ci.org/Maratyszcza/pthreadpool) 5 6**pthreadpool** is a portable and efficient thread pool implementation. 7It provides similar functionality to `#pragma omp parallel for`, but with additional features. 8 9## Features: 10 11* C interface (C++-compatible). 12* 1D-6D loops with step parameters. 13* Run on user-specified or auto-detected number of threads. 14* Work-stealing scheduling for efficient work balancing. 15* Wait-free synchronization of work items. 16* Compatible with Linux (including Android), macOS, iOS, Windows, Emscripten environments. 17* 100% unit tests coverage. 18* Throughput and latency microbenchmarks. 19 20## Example 21 22 The following example demonstates using the thread pool for parallel addition of two arrays: 23 24```c 25static void add_arrays(struct array_addition_context* context, size_t i) { 26 context->sum[i] = context->augend[i] + context->addend[i]; 27} 28 29#define ARRAY_SIZE 4 30 31int 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]; 35 36 pthreadpool_t threadpool = pthreadpool_create(0); 37 assert(threadpool != NULL); 38 39 const size_t threads_count = pthreadpool_get_threads_count(threadpool); 40 printf("Created thread pool with %zu threads\n", threads_count); 41 42 struct array_addition_context context = { augend, addend, sum }; 43 pthreadpool_parallelize_1d(threadpool, 44 (pthreadpool_task_1d_t) add_arrays, 45 (void*) &context, 46 ARRAY_SIZE, 47 PTHREADPOOL_FLAG_DISABLE_DENORMALS /* flags */); 48 49 pthreadpool_destroy(threadpool); 50 threadpool = NULL; 51 52 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]); 58 59 return 0; 60} 61``` 62