Kévin Dunglas ec35afdc7f chore: remove C-Thread-Pool/example.c (#259) | 1 year ago | |
---|---|---|
.. | ||
LICENSE | 2 years ago | |
README.md | 2 years ago | |
thpool.c | 1 year ago | |
thpool.h | 2 years ago |
This is a minimal but advanced threadpool implementation.
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!
If this project reduced your development time feel free to buy me a coffee.
The library is not precompiled so you have to compile it with your project. The thread pool
uses POSIX threads so if you compile with gcc on Linux you have to use the flag -pthread
like this:
gcc example.c thpool.c -D THPOOL_DEBUG -pthread -o example
Then run the executable like this:
./example
#include "thpool.h"
threadpool thpool = thpool_init(4);
thpool_add_work(thpool, (void*)function_p, (void*)arg_p);
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
you can use thpool_wait(thpool);
. If you want to destroy the pool you can use
thpool_destroy(thpool);
.
For a deeper look into the documentation check in the thpool.h file. Below is a fast practical overview.
Function example | Description |
---|---|
thpool_init(4) | Will return a new threadpool with 4 threads. |
thpool_add_work(thpool, (void*)function_p, (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. |
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_resume(thpool) | If the threadpool is paused, then all threads will resume from where they were. |
thpool_num_threads_working(thpool) | Will return the number of currently working threads. |
You are very welcome to contribute. If you have a new feature in mind, you can always open an issue on github describing it so you don't end up doing a lot of work that might not be eventually merged. Generally we are very open to contributions as long as they follow the below keypoints.
If you wish to get access as a collaborator feel free to mention it in the issue https://github.com/Pithikos/C-Thread-Pool/issues/78