Table of Contents


qt_loop_balance, qt_loop_balance_future - a slightly intelligent implementation of a threaded loop


#include <qthread/qloop.h>

qt_loop_balance (const size_t start, const size_t stop, const qt_loop_f func, void *argptr);

qt_loop_balance_future (const size_t start, const size_t stop, const qt_loop_f func, void *argptr);


This function provides a simple C implementation of a threaded loop. Rather than using a predictable number of threads, however, the number of threads generated depends directly upon the number of shepherds available. The difference between qt_loop_balance() and qt_loop_balance_future() is that the former uses qthreads and the latter uses futures.

One qthread (or future) is spawned for each shepherd. The set of values of i (iterations) is divided evenly among the shepherds, and each qthread (or future) 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() and qt_loop_balance_future() will not return until all of the qthreads (or futures) it spawned have exited.

See Also

qt_loop(3) , qt_loop_future(3) , qt_loopaccum_balance(3) , qt_loopaccum_balance_future(3)

Table of Contents