patrickmacarthur / tpool Goto Github PK
View Code? Open in Web Editor NEWA simple thread pool package for POSIX systems inspired by Java's java.util.concurrent package.
License: Other
A simple thread pool package for POSIX systems inspired by Java's java.util.concurrent package.
License: Other
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]>
Test the library with a very simple test case and make sure it works.
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.
Forgot to destroy thread pool mutex in tpool_destroy()
I envision the following three scenarios:
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.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
).
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.