|
Objectively
Object oriented framework for C.
|
#include <ThreadPool.h>
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
Submitted functions wait in a first-in, first-out queue until a Thread is free. Each ThreadFunction receives a Thread whose data is the user data that was submitted with the function.
Deallocating a ThreadPool sets isCancelled on its Threads, which then run the functions still in the queue before they exit. A long-running function MAY check isCancelled to return early. A function MUST NOT cancel the Thread that it is given.
A function runs on the calling thread, with a Thread that is never started and never cancelled, in two cases: ThreadPool::wait is called while the function is still in the queue, or the size of the ThreadPool is 0. A function MUST NOT join, kill or detach that Thread, and MUST NOT rely on isCancelled alone to return, because it is never set.
Definition at line 64 of file ThreadPool.h.
Properties | |
| Object | object |
| The superclass. | |
| size_t | size |
| The number of Threads. | |
Properties inherited from Object | |
| Class * | clazz |
| Every instance of Object begins with a pointer to its Class. | |
| unsigned int | magic |
| A header to allow introspection of Object types. | |
Methods | |
| Class * | _ThreadPool (void) |
| The ThreadPool archetype. | |
| void | detach (ThreadPool *self, ThreadFunction function, ident data) |
Queues function without a handle to wait on. | |
| ThreadPoolWork * | execute (ThreadPool *self, ThreadFunction function, ident data) |
Queues function, returning a handle to wait on. | |
| ThreadPool * | init (ThreadPool *self) |
| Initializes this ThreadPool with one Thread per online processor. | |
| ThreadPool * | initWithSize (ThreadPool *self, size_t size) |
| Initializes this ThreadPool with the specified number of Threads. | |
| ident | wait (ThreadPool *self, ThreadPoolWork *work) |
| Waits for the specified ThreadPoolWork to finish, and frees it. | |
Methods inherited from Object | |
| Class * | _Object (void) |
| The Object archetype. | |
| Object * | copy (const Object *self) |
| Creates a shallow copy of this Object. | |
| void | dealloc (Object *self) |
| Frees all resources held by this Object. | |
| String * | description (const Object *self) |
| int | hash (const Object *self) |
| Object * | init (Object *self) |
| Initializes this Object. | |
| bool | isEqual (const Object *self, const Object *other) |
| Tests equality of the other Object. | |
| bool | isKindOfClass (const Object *self, const Class *clazz) |
| Tests for Class hierarchy membership. | |
Protected Attributes | |
| ThreadPoolInterface * | interface [0] |
| The interface type. | |
Protected Attributes inherited from Object | |
| ObjectInterface * | interface [0] |
| The interface type. | |
|
protected |
The interface type.
Definition at line 75 of file ThreadPool.h.
| Object ThreadPool::object |
The superclass.
Definition at line 69 of file ThreadPool.h.
| size_t ThreadPool::size |
| Class * _ThreadPool | ( | void | ) |
The ThreadPool archetype.
Definition at line 375 of file ThreadPool.c.
| void detach | ( | ThreadPool * | self, |
| ThreadFunction | function, | ||
| ident | data | ||
| ) |
Queues function without a handle to wait on.
| self | The ThreadPool. |
| function | The ThreadFunction. |
| data | The user data, available to function as thread->data. |
function is discarded. Definition at line 184 of file ThreadPool.c.
| ThreadPoolWork * execute | ( | ThreadPool * | self, |
| ThreadFunction | function, | ||
| ident | data | ||
| ) |
Queues function, returning a handle to wait on.
| self | The ThreadPool. |
| function | The ThreadFunction. |
| data | The user data, available to function as thread->data. |
Definition at line 192 of file ThreadPool.c.
| ThreadPool * init | ( | ThreadPool * | self | ) |
Initializes this ThreadPool with one Thread per online processor.
| self | The ThreadPool. |
NULL on error. Definition at line 200 of file ThreadPool.c.
| ThreadPool * initWithSize | ( | ThreadPool * | self, |
| size_t | size | ||
| ) |
Initializes this ThreadPool with the specified number of Threads.
| self | The ThreadPool. |
| size | The number of Threads. With 0, every function runs on the calling thread. |
NULL on error. Definition at line 290 of file ThreadPool.c.
| ident wait | ( | ThreadPool * | self, |
| ThreadPoolWork * | work | ||
| ) |
Waits for the specified ThreadPoolWork to finish, and frees it.
| self | The ThreadPool. |
| work | The ThreadPoolWork returned by ThreadPool::execute, or NULL. |
NULL if work is NULL. work is still in the queue, it runs on the calling thread. So a ThreadFunction MAY wait on other work of its own ThreadPool without a deadlock.