Giter Site home page Giter Site logo

c-thread-pool's Introduction

Build status

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!

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.

c-thread-pool's People

Contributors

pithikos avatar trashman2 avatar medicineyeh avatar nil0x42 avatar biw avatar dweymouth avatar marwankallal avatar bpetri avatar chuangxie avatar bowfin avatar xiaokai-wang avatar lyingbug avatar zweng avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.