ASL
Loading...
Searching...
No Matches
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:216

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();

The static functions Thread::parallel_for() and Thread::parallel_invoke() are deprecated. You can probably just use OpenMP.

See also Mutex, Semaphore and Lock.

#include <asl/Thread.h>

Public Member Functions

virtual void run ()
 The thread procedure.
void start ()
 Starts a new thread by calling run() in parallel.
void kill ()
 Cancels a thread, but you should not normally do this.
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, bool detached=false)
 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.
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.

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()

template<class F>
void parallel_for ( int i0,
int i1,
const F & f,
int nth = 8 )
static

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.

Is equivalent to running:

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

but with iterations run in nth parallel threads.

Deprecated
Use OpenMP

◆ parallel_invoke() [1/2]

template<class F1, class F2>
void parallel_invoke ( const F1 & f1,
const F2 & f2 )
static

Runs the two functions/lambdas in parallel and returns when both are finished.

Deprecated
Use OpenMP

◆ parallel_invoke() [2/2]

template<class F1, class F2, class F3>
void parallel_invoke ( const F1 & f1,
const F2 & f2,
const F3 & f3 )
static

Runs the 3 functions/lambdas in parallel and returns when all are finished.

Deprecated
Use OpenMP

◆ 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:
  • Thread.h