ASL
Thread Class Reference

Detailed Description

The Thread class represents an execution thread.

To create threads, derive a class from Thread and reimplement the run() function. That is what objects of the new class will execute in parallel when the start() function is called.

struct Worker : public Thread
{
void run()
{
... // what the function will do in a parallel thread
}
};
Worker worker;
worker.start();
...
worker.join();
virtual void run()
The thread procedure.
Definition: Thread.h:203

In compilers supporting lambdas threads can now be started with a lambda without defining a specific Thread subclass. These threads can get input data or keep state using variables captured by the lambda.

Thread thread([&]() {
... // what the thread does
});
...
thread.join();

See also Mutex, Semaphore and Lock.

#include <Thread.h>

Public Member Functions

virtual void run ()
 The thread procedure. More...
 
void start ()
 Starts a new thread by calling run() in parallel.
 
void kill ()
 Cancels a thread, but you should not normally do this. More...
 
void join ()
 Waits for this thread to end.
 
bool finished () const
 Returns true if this thread has finished.
 
template<class F >
 Thread (const F &f)
 Starts a thread given a function or lambda expression as the run() function.
 

Static Public Member Functions

static int numProcessors ()
 Returns the number of logical processors or cores.
 
template<class F >
static void parallel_for (int i0, int i1, const F &f, int nth=8)
 Emulates an OpenMP parallel for by running function f several times in parallel. More...
 
template<class F1 , class F2 >
static void parallel_invoke (const F1 &f1, const F2 &f2)
 Runs the two functions/lambdas in parallel and returns when both are finished.
 
template<class F1 , class F2 , class F3 >
static void parallel_invoke (const F1 &f1, const F2 &f2, const F3 &f3)
 Runs the 3 functions/lambdas in parallel and returns when all are finished.
 
template<class F1 , class F2 , class F3 , class F4 >
static void parallel_invoke (const F1 &f1, const F2 &f2, const F3 &f3, const F4 &f4)
 Runs the 4 functions/lambdas in parallel and returns when all are finished.
 

Member Function Documentation

◆ kill()

void kill ( )
inline

Cancels a thread, but you should not normally do this.

Deprecated:
Threads should end when their function ends

◆ parallel_for()

static void parallel_for ( int  i0,
int  i1,
const F &  f,
int  nth = 8 
)
inlinestatic

Emulates an OpenMP parallel for by running function f several times in parallel.

Function f must receive an integer argument which will get the values from i0 up to but not including i1.

Thread::parallel_for(i0, i1, [&](int i) {
f(i);
});
static void parallel_for(int i0, int i1, const F &f, int nth=8)
Emulates an OpenMP parallel for by running function f several times in parallel.
Definition: Thread.h:296

Is equivalent to running:

for(int i=i0; i<i1; i++) {
f(i);
}

but with iterations run in nth parallel threads.

◆ run()

virtual void run ( )
inlinevirtual

The thread procedure.

Reimplement this function to create new threads


The documentation for this class was generated from the following file: