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