qt_loop_balance - Man Page

a slightly intelligent implementation of a threaded loop

Synopsis

#include <qthread/qloop.h>

void
qt_loop_balance (const size_t start, const size_t stop,

const qt_loop_f func, void *argptr);

void
qt_loop_balance_simple (const size_t start, const size_t stop,

const qt_loop_f func, void *argptr);

Description

These functions provide a simple C implementation of a threaded loop. Rather than using a fixed number of threads, however, the number of threads generated depends directly upon the number of shepherds available. The qt_loop_balance_simple() variant uses simple qthreads, which cannot block or yield, but receive full-size stacks and execute as function-calls from the worker threads, thereby avoiding most context-swap overheads.

One qthread is spawned for each shepherd. The set of values of i (iterations) is divided evenly among the shepherds, and each qthread is assigned a set of iterations to perform.

The func argument must be a function pointer with a qt_loop_f prototype. Its basic code structure is expected to look like this:

void func (const size_t startat, const size_t stopat, void *arg)
{

for (size_t i = startat; i < stopat; ++i) {

/* do work */

}

}

The arguments startat and stopat are determined by the library, and tell the function what range of i values (iterations) it is responsible for. qt_loop_balance() will not return until all of the qthreads it spawned have exited.

See Also

qt_loop(3), qt_loopaccum_balance(3), qthread_spawn(3)

Referenced By

qt_double_max(3), qt_double_min(3), qt_double_prod(3), qt_double_sum(3), qt_loop(3), qt_loopaccum_balance(3), qt_loop_queue_addworker(3), qt_loop_queue_run(3), qt_loop_queue_setchunk(3).

APRIL 2011 libqthread