Code Monkey home page Code Monkey logo

tpool's Introduction

tpool is a simple thread pool library built on top of POSIX threads.
When a task is submitted to the thread pool, the library will spawn a thread
to run the task and/or queue the task for later execution.  The number of
threads in the thread pool is dynamic but the user specifies a fixed maximum
size at startup time.

When initialized with tpool_init(), the thread pool will have 0 threads
running.  When a task is added to the pool with tpool_submit(), the pool will
spawn a thread for that task.  For subsequent tasks, if the pool has not
reached its maximum size, it will spawn a new thread; otherwise, it will
queue the task and an existing thread will take the task when it finishes a
previous task.

When a task is submitted with tpool_submit(), a pointer to a FUTURE may be
provided.  Its value will not be available until the task finishes.  The value
may be obtained via future_get(), which may block until the value is available.
After the value has been obtained, the future should be destroyed with
future_destroy().

The thread pool may be shut down with tpool_shutdown(), in which case it will
refuse any new tasks.  All running and queued tasks will run to completion.
When they have run to completion, resources used by the thread pool can be
freed with tpool_destroy().

tpool is currently maintained by:
Patrick MacArthur <[email protected]>

tpool's People

Contributors

patrickmacarthur avatar

Stargazers

Duan Wenbo avatar  avatar

Watchers

 avatar  avatar

tpool's Issues

Test library

Test the library with a very simple test case and make sure it works.

BUG: possible deadlock in future_destroy()

If future_destroy() is called when the value is not yet ready, it will return EBUSY without releasing the lock. This means that any subsequent calls to future_get() will block forever regardless of the status of the future value.

Give user some control over scheduling in tpool_submit()

I envision the following three scenarios:

  1. default case: This is the way that it is right now. A new thread would be spawned for the task if there are more threads available; otherwise, the task would be queued.
  2. queued mode (e.g., task->flags & TPOOL_TASK_QUEUED): The task would be always queued unless there are no threads currently running in the pool, in which case a thread would be spawned for the task.
  3. immediate exclusive mode (e.g., task->flags & TPOOL_TASK_IMMEDIATE): A new thread would be spawned for the task if there are more threads available; otherwise, tpool_submit() would return EBUSY.

Flag names aren't set in stone yet. If the two constants that I used above are used, then TPOOL_TASK_QUEUED and TPOOL_TASK_IMMEDIATE will be mutually exclusive (if used together, tpool_submit would return EINVAL).

Make all API functions non-blocking by default

Right now, some API calls are non-blocking and others are blocking. The idea would be to make all API calls non-blocking by default (so that you could, for example, check on a task, and if the task is not completed yet, do something else). A TPOOL_WAIT flag would be added and supported by most API functions (where it makes sense) so that you could also make a blocking call if you wanted.

Export a struct tpool_task, and use it for tpool_submit()

tpool_submit() takes 5 arguments currently, one of which is optional if the previous argument is not supplied. It may be clearer if we just export a struct to the user, and have the user fill in the struct and then pass the struct in as arguments. This would make this function similar to sigaction(2).

Thus, the new signature would be

int
tpool_submit(TPOOL *tpool, struct tpool_task *task, FUTURE **future)
{
}

and the struct would most likely contain:

struct tpool_task {
        void *(*task)(void *);
        void *arg;
        int flags;
};

This would also require that the queue create its own internal chain structure that also kept track of associated internal structures for each task.

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.