Git Product home page Git Product logo

cpool's Introduction

Introduction

cpool is a simple C thread pool library implemented with C11 <threads.h>.

A ring buffer is used as a bounded job queue. All worker threads are started on pool creation.

Future

cpool has a useful little feature somewhat akin to std::future in C++. When enqueuing a job, you can optionally receive a future handle associated with the job. This provides an easy way to wait on individual jobs, without the need for manual synchronization.

Example usage

#include "cpool.h"
#include <stdio.h>
#include <threads.h>
#include <stdlib.h>

static void sleep(void* arg)
{
    (void)arg;
    thrd_sleep(&(struct timespec) { .tv_sec = 1 }, NULL);
}

static void print_int(void* arg)
{
    thrd_sleep(&(struct timespec) { .tv_sec = 1 }, NULL);
    printf("%d\n", *(int*)arg);
    free(arg);
}

int main(void)
{
    cpool* tp = cpool_create(8, 8); // creates pool with 8 worker threads, job queue capacity 8.
    if (!tp) return EXIT_FAILURE;

    cpool_future* fut;
    cpool_enqueue(tp, sleep, NULL, &fut); // Receive a future handle

    if (fut) {
        puts("Waiting for future...");
        cpool_wait_future(fut);
    }
    puts("Waiting for pool...");
    cpool_wait(tp);
    puts("pool finished");

    for (int i = 0; i < 20; ++i) {
        int* arg = malloc(sizeof(arg));
        if (!arg) break;
        *arg = i;
        cpool_enqueue(tp, print_int, arg, NULL); // enqueue w/o using future.
                                                 // This will not incur extra overhead of future.
    }

    cpool_wait(tp);
    puts("pool finished");

    cpool_destroy(tp);
    puts("pool destroyed");
}

cpool's People

Contributors

kya8 avatar

Watchers

 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.