DD::Image::Thread Class Reference

List of all members.

Public Types

typedef void(* ThreadFunction )(unsigned index, unsigned nThreads, void *userData)

Static Public Member Functions

static bool spawn (ThreadFunction, unsigned nThreads, void *userData)
static ThreadId GetThreadId ()
static const ThreadInfo * thisThread ()
static int thisIndex ()
static bool OnMainThread ()
 returns true if we're on the main-thread, false otherwise
static void terminate_all ()
static bool running (void *userData)
static void wait (void *userData)
static RunningThreadsInfo getRunningThreadsInfo ()

Static Public Attributes

static unsigned numCPUs
 recommended count for compute-only threads
static unsigned numThreads
 recommended count for threads that will do I/O
static size_t gMinStackSize
 the minimum stack size each pthread can have
static const size_t kAbsoluteMinStackSize
 absolute minimum stack size permissible

Detailed Description

Namespace of functions to create and wait for parallel threads.

"Real" threads as seen by the operating system are reused. After the function passed to spawn() returns, the thread will wait until another spawn() is called and will pick up the function from that. Therefore the operating system overhead of creating and destroying threads is avoided and you can call spawn() as much as you want even with short functions.

For synchronization see the classes Lock, SignalLock, and Guard.


Member Function Documentation

bool DD::Image::Thread::spawn ( ThreadFunction  function,
unsigned  nThreads,
void *  userData 
) [static]

Start nThreads parallel threads, all running function. The arguments to function are an index number between 0 and nThreads-1, nThreads, and the userData argument.

References gMinStackSize, DD::Image::Lock::lock(), and DD::Image::Lock::unlock().

Referenced by DD::Image::ImagePlane::fillChannelThreaded(), and DD::Image::ToBuffer::to_buffer().

ThreadId DD::Image::Thread::GetThreadId ( ) [static]

Returns an OS agnostic thread ID.

Referenced by thisThread().

const Thread::ThreadInfo * DD::Image::Thread::thisThread ( ) [static]

Returns a pointer to a structure containing the arguments passed to the spawn() that launched this thread, and the index number passed to the function for this thread. This pointer is good until the thread function exits.

Returns null if this thread does not appear to have been launched by spawn(), this is usually because you are in the main thread.

References GetThreadId(), DD::Image::Lock::lock(), and DD::Image::Lock::unlock().

void DD::Image::Thread::terminate_all ( ) [static]

Destroys all the threads. This is used on program exit to because some systems (Linux?) don't seem to get rid of parallel threads on normal exit!

References DD::Image::Lock::lock(), and DD::Image::Lock::unlock().

bool DD::Image::Thread::running ( void *  userData) [static]

Return true if any threads launched by a call to spawn() with the given userData are still running.

References DD::Image::Lock::lock(), and DD::Image::Lock::unlock().

void DD::Image::Thread::wait ( void *  userData) [static]

Wait until all threads launched by a call to spawn() with the given userData have exited.

References DD::Image::Lock::lock(), and DD::Image::Lock::unlock().

Referenced by DD::Image::ImagePlane::fillChannelThreaded(), DD::Image::ToBuffer::to_buffer(), and DD::Image::Interest::~Interest().

Thread::RunningThreadsInfo DD::Image::Thread::getRunningThreadsInfo ( ) [static]

Get info on the running threads

References DD::Image::Lock::lock(), and DD::Image::Lock::unlock().