Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* threadpool.h
*
* A work-stealing, fork-join thread pool.
*/
/*
* Opaque forward declarations. The actual definitions of these
* types will be local to your threadpool.c implementation.
*/
struct thread_pool;
struct future;
/* Create a new thread pool with no more than n threads. */
struct thread_pool * thread_pool_new(int nthreads);
/*
* Shutdown this thread pool in an orderly fashion.
* Tasks that have been submitted but not executed may or
* may not be executed.
*
* Deallocate the thread pool object before returning.
*/
void thread_pool_shutdown_and_destroy(struct thread_pool *);
/* A function pointer representing a 'fork/join' task.
* Tasks are represented as a function pointer to a
* function.
* 'pool' - the thread pool instance in which this task
* executes
* 'data' - a pointer to the data provided in thread_pool_submit
*
* Returns the result of its computation.
*/
typedef void * (* fork_join_task_t) (struct thread_pool *pool, void * data);
/*
* Submit a fork join task to the thread pool and return a
* future. The returned future can be used in future_get()
* to obtain the result.
* 'pool' - the pool to which to submit
* 'task' - the task to be submitted.
* 'data' - data to be passed to the task's function
*
* Returns a future representing this computation.
*/
struct future * thread_pool_submit(
struct thread_pool *pool,
fork_join_task_t task,
void * data);
/* Make sure that the thread pool has completed the execution
* of the fork join task this future represents.
*
* Returns the value returned by this task.
*/
void * future_get(struct future *);
/* Deallocate this future. Must be called after future_get() */
void future_free(struct future *);