• Stars
    star
    2,063
  • Rank 22,400 (Top 0.5 %)
  • Language
    C
  • License
    MIT License
  • Created about 13 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal but powerful thread pool in ANSI C

CircleCI

C Thread Pool

This is a minimal but advanced threadpool implementation.

  • ANCI C and POSIX compliant
  • Pause/resume/wait as you like
  • Simple easy-to-digest API
  • Well tested

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.

Donate

Run an example

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

Basic usage

  1. Include the header in your source file: #include "thpool.h"
  2. Create a thread pool with number of threads you want: threadpool thpool = thpool_init(4);
  3. Add work to the pool: 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);.

API

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.

Contribution

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.

  • Try to keep the API as minimal as possible. That means if a feature or fix can be implemented without affecting the existing API but requires more development time, then we will opt to sacrifice development time.
  • Solutions need to be POSIX compliant. The thread-pool is advertised as such so it makes sense that it actually is.
  • For coding style simply try to stick to the conventions you find in the existing codebase.
  • Tests: A new fix or feature should be covered by tests. If the existing tests are not sufficient, we expect an according test to follow with the pull request.
  • Documentation: for a new feature please add documentation. For an API change the documentation has to be thorough and super easy to understand.

If you wish to get access as a collaborator feel free to mention it in the issue #78

More Repositories

1

python-websocket-server

A simple fully working websocket-server in Python with no external dependencies
Python
1,147
star
2

docker-enter

A docker tool to enter an existing container. The tools relies on docker and setns.
C
84
star
3

rest-framework-roles

Role-based permissions for Django and Django REST Framework
Python
56
star
4

python-rectangles

Generic rectangles in screen coordinates. Functions to test relations between rectangles are supported like overlap, distance, etc.
Python
26
star
5

Geoexplorer

A testing and development tool for developers working with Google Places, Foursquare and other geo-services.
Python
26
star
6

winlaunch

Save the position of open windows and auto-generate scripts to launch them
Python
17
star
7

django-compressor-postcss

PostCSS support for django-compressor
Python
16
star
8

python-reusables

Reusable code for Python so I don't have to write the same thing twice!
Python
11
star
9

php-table-class

PHP Table class with minimal interface
PHP
11
star
10

Dockerfiles

A set of documented Dockerfiles
Shell
5
star
11

debian-scripts-and-bashisms

Scripts and files to ease the pain of getting a fresh new Ubuntu install to a ready dev machine.
Python
4
star
12

bootchain

Run bootstrap scripts recursively
Shell
3
star
13

mbed-win-ls

Show connected mbed boards to a Windoze machine
Python
2
star
14

rest-datasource

REST Datasource for Grafana
TypeScript
2
star
15

Google-Places-v3-Python3-wrapper

A wrapper for Google Places v3 - Python3
Python
2
star
16

mbed-tools

A set of tools/scripts that can be of benefit to anyone playing with mbed boards
Python
2
star
17

bash-contexter

No need to type the full commands in the terminal anymore
Shell
2
star
18

mbed_lmtools

Playground to create lmtools for mbed
Python
1
star
19

docummander

A Jekyll template for documenting projects easily via the web browser
CSS
1
star