C Collections

cthreadpool · Section 3

cthrpool_submit_future

cthrpool_submit_future - submit a task that returns a void* result (blocking)

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadpool.h>
 
cthrpool_future *cthrpool_submit_future(cthrpool pool,
                                          void *(*fn)(void *),
                                          void *arg,
                                          ccol_retval *rv_out);

Allocates a future object and submits a task to the pool. The task
calls fn(arg) on a worker thread and stores the void* that fn returned
in the future, where cthrpool_future_get can read it later.
 
On success the future has a reference count of 2: The caller holds one
reference, and the queued task holds the other inside the library.
Each cthrpool_future_get call that runs on the future adds one more. The
caller must call cthrpool_future_free exactly once when it is done.
 
With a bounded queue the function blocks until the queue has a free
slot; use cthrpool_try_submit_future or cthrpool_timed_submit_future if you
don't want it to block.
 
A call from a task running on this same pool, or from the on_complete
callback of that task, while a bounded queue is already at capacity,
fails with ccol_container_full instead of waiting, for the reason
cthrpool_submit(3) gives: Only the workers of this pool can free a slot,
and the caller is one of them. An unbounded queue never gets into that
case.
 
When rv_out is not NULL the function stores a result code there on
every return, so a caller can tell the reasons for a NULL return
apart. The codes are the ones cthrpool_submit(3) returns for the same
conditions.
 
A future always uses the standard malloc and free, never a custom
allocator that the caller set when creating the pool.

pool    Thread pool handle
fn      Task function returning void* (must not be NULL)
arg     Passed to fn (may be NULL)
rv_out  Receives the result code on every return (may be NULL)

Returns a new cthrpool_future handle on success, and stores
ccol_success in *rv_out.
Returns NULL on failure, and stores one of these codes in *rv_out:
  ccol_invalid_args       pool is CTHRPOOL_NULL, or a stale and
                          already destroyed handle, or fn is NULL
  ccol_not_permitted      the pool is in shutdown
  ccol_container_full     a bounded queue is at capacity and the
                          caller is a worker of this same pool
  ccol_not_enough_memory  a memory allocation fails

cthrpool_submit(3), cthrpool_try_submit_future(3), cthrpool_timed_submit_future(3), cthrpool_future_get(3), cthrpool_future_done(3), cthrpool_future_cancelled(3), cthrpool_future_free(3)