2015-01-17 14:46:49 +03:00
# C Thread Pool
2015-01-17 15:04:17 +03:00
This is a minimal but fully functional threadpool implementation.
2011-11-05 16:35:31 +04:00
2015-01-17 14:35:57 +03:00
* ANCI C and POSIX compliant
2015-01-17 15:04:51 +03:00
* Number of threads can be chosen on initialization
2015-01-17 15:07:59 +03:00
* Minimal but powerful interface
2015-01-17 14:35:57 +03:00
* Full documentation
2015-01-17 15:11:00 +03:00
The threadpool is under MIT license. Notice that this project took a considerable amount of work and sacrifice of my free time and the reason I give it for free (even for commercial use) is so when you become rich and wealthy you don't forget about us open-source creatures of the night. Cheers!
2015-01-17 14:35:57 +03:00
2011-11-05 16:35:31 +04:00
2015-01-18 02:10:15 +03:00
## v2 Changes
2015-01-17 14:46:49 +03:00
2015-01-18 02:10:15 +03:00
This is an updated and heavily refactored version of my original threadpool. The main things taken into consideration in this new version are:
2015-01-17 14:35:57 +03:00
2015-01-17 15:07:59 +03:00
* Synchronisation control from the user (pause/resume/wait)
2015-01-17 14:35:57 +03:00
* Thorough testing for memory leaks and race conditions
* Cleaner and more opaque API
2015-01-16 22:38:22 +03:00
## Compiling
2014-08-19 19:00:55 +04:00
2011-11-05 16:35:31 +04:00
The library is not precompiled so you have to compile it with your project. The thread pool
2015-01-18 02:10:15 +03:00
uses POSIX threads so if you compile with gcc on Linux you have to use the flag `-pthread` like this:
2011-11-05 16:35:31 +04:00
2015-01-17 15:09:52 +03:00
gcc example.c thpool.c -pthread -o example
2011-11-05 16:35:31 +04:00
Then run the executable like this:
2015-01-17 15:09:52 +03:00
./example
2011-11-05 16:35:31 +04:00
2015-01-17 14:46:49 +03:00
##Basic usage
2011-11-05 16:35:31 +04:00
2015-01-17 14:46:49 +03:00
1. Include the header in your source file: ``#include "thpool.h"`
2015-01-18 02:10:15 +03:00
2. Create a thread pool with number of threads you want: `threadpool thpool = thpool_init(4);`
2015-01-17 14:46:49 +03:00
3. Add work to the pool: `thpool_add_work(thpool, (void*)doSth, (void*)arg);`
2014-12-30 15:52:14 +03:00
2015-01-18 02:10:15 +03:00
The workers(threads) will start their work automatically as fast as there is new work
in the pool. If you want to wait for all added work to be finished before continuing
2014-12-30 15:52:14 +03:00
you can use `thpool_wait(thpool);` . If you want to destroy the pool you can use
`thpool_destroy(thpool);` .
2011-11-05 16:35:31 +04:00
2015-01-17 14:35:57 +03:00
2015-01-17 23:08:46 +03:00
##API
2014-09-21 19:40:20 +04:00
2015-01-18 02:05:37 +03:00
For a deeper look into the documentation check in the `thpool.h` file. Below is a fast practical overview.
2015-01-17 14:35:57 +03:00
2015-01-18 02:04:39 +03:00
| Function example | Description |
|---------------------------------|---------------------------------------------------------------------|
| ** *thpool_init(4)*** | Will return a new threadpool with 4 threads. |
| ** *thpool_add_work(thpool, void *(*function_p)(void*), (void*)arg_p)*** | Will add new work to the pool. Work is simply a function. You can pass a single argument to the function if you wish. If not, NULL should be passed. |
2015-01-18 02:07:24 +03:00
| ** *thpool_wait(thpool)*** | Will wait for all jobs (both in queue and currently running) to finish. |
| ** *thpool_destroy(thpool)*** | This will destroy the threadpool. If jobs are currently being executed, then it will wait for them to finish. |
| ** *thpool_pause(thpool)*** | All threads in the threadpool will pause no matter if they are idle or executing work. |
| ** *thpool_pause(thpool)*** | If the threadpool is paused, then all threads will resume from where they were. |