Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::Thread Class Referenceabstract

Utility class to execute code in a separate execution thread. More...

#include <thread.h>

+ Collaboration diagram for lunchbox::Thread:

Public Types

enum  Affinity { NONE = 0, CORE = 1, SOCKET = -65536, SOCKET_MAX = -1024 }
 Enumeration values for thread affinity. More...
 

Public Member Functions

 Thread ()
 Construct a new thread. More...
 
 Thread (const Thread &from)
 Copy constructor. More...
 
virtual ~Thread ()
 Destruct the thread. More...
 
virtual bool start ()
 Start the thread. More...
 
virtual bool init ()
 The init function for the child thread. More...
 
virtual void run ()=0
 The entry function for the child thread. More...
 
virtual void exit ()
 Exit the child thread immediately. More...
 
void cancel ()
 Cancel (stop) the child thread. More...
 
bool join ()
 Wait for the exit of the child thread. More...
 
bool isStopped () const
 Return if the thread is stopped. More...
 
bool isRunning () const
 Return if the thread is running. More...
 
bool isCurrent () const
 

Static Public Member Functions

static ThreadID getSelfThreadID ()
 
static void yield ()
 
static void setName (const std::string &name)
 
static void setAffinity (const int32_t affinity)
 

Friends

void abort (bool)
 

Detailed Description

Utility class to execute code in a separate execution thread.

Deprecated:
Use Boost.Thread

Example:

/* Copyright (c) 2006-2014, Stefan Eilemann <eile@equalizergraphics.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iostream>
#include <lunchbox/clock.h>
#include <lunchbox/sleep.h>
#include <lunchbox/test.h>
#include <lunchbox/thread.h>
#define NTHREADS 256
class LoadThread : public lunchbox::Thread
{
public:
virtual ~LoadThread() {}
virtual void run() {}
};
class InitThread : public LoadThread
{
public:
InitThread()
: initLeft(false)
{
}
virtual ~InitThread() {}
virtual bool init()
{
initLeft = true;
return true;
}
virtual void run()
{
TEST(!join());
exit();
}
bool initLeft;
};
class FailThread : public InitThread
{
public:
virtual ~FailThread() {}
virtual bool init() { return false; }
};
int main(int, char**)
{
LoadThread loadThreads[NTHREADS];
for (size_t i = 0; i < NTHREADS; ++i)
TEST(loadThreads[i].start());
for (size_t i = 0; i < NTHREADS; ++i)
TEST(loadThreads[i].join());
const float time = clock.getTimef();
std::cout << "Spawned and joined " << NTHREADS << " loadThreads in " << time
<< " ms (" << (NTHREADS / time) << " threads/ms)" << std::endl;
for (size_t i = 0; i < NTHREADS; ++i)
TEST(loadThreads[i].isStopped());
InitThread initThreads[NTHREADS];
clock.reset();
for (size_t i = 0; i < NTHREADS; ++i)
{
TEST(initThreads[i].start());
TEST(initThreads[i].initLeft == true);
}
#ifdef _MSC_VER // resolution of Sleep is not high enough...
TESTINFO(clock.getTimef() + 1.f > NTHREADS * 10, clock.getTimef());
#else
TESTINFO(clock.getTimef() > NTHREADS * 10, clock.getTimef());
#endif
for (size_t i = 0; i < NTHREADS; ++i)
TEST(initThreads[i].join());
FailThread failThread;
TEST(!failThread.start());
TEST(!failThread.isRunning());
TEST(failThread.isStopped());
TEST(!failThread.join());
return EXIT_SUCCESS;
}

Definition at line 44 of file thread.h.

Member Enumeration Documentation

Enumeration values for thread affinity.

Enumerator
NONE 

Don't set any affinity.

CORE 

Bind to a specific CPU core.

SOCKET 

Bind to all cores of a specific socket (CPU)

SOCKET_MAX 

Highest bindable CPU.

Definition at line 48 of file thread.h.

Constructor & Destructor Documentation

lunchbox::Thread::Thread ( )

Construct a new thread.

Version
1.0
lunchbox::Thread::Thread ( const Thread from)
explicit

Copy constructor.

Version
1.1.2
virtual lunchbox::Thread::~Thread ( )
virtual

Destruct the thread.

Version
1.0

Member Function Documentation

void lunchbox::Thread::cancel ( )

Cancel (stop) the child thread.

This function is not to be called from the child thread.

Version
1.0
virtual void lunchbox::Thread::exit ( )
virtual

Exit the child thread immediately.

This function does not return. It is only to be called from the child thread.

Version
1.0
static ThreadID lunchbox::Thread::getSelfThreadID ( )
static
Returns
a unique identifier for the calling thread.
Version
1.0

Referenced by init().

+ Here is the caller graph for this function:

virtual bool lunchbox::Thread::init ( )
inlinevirtual

The init function for the child thread.

The parent thread will not be unlocked before this function has been executed. If the thread initialization fails, that is, this method does return false, the thread will be stopped and the start() method will return false.

Returns
the success value of the thread initialization.
Version
1.0

Definition at line 86 of file thread.h.

References lunchbox::exit(), getSelfThreadID(), and lunchbox::operator<<().

+ Here is the call graph for this function:

bool lunchbox::Thread::isCurrent ( ) const
Returns
true if the calling thread is the same thread as this thread, false otherwise.
Version
1.0
bool lunchbox::Thread::isRunning ( ) const

Return if the thread is running.

Note that the thread may be neither running nor stopped if it is currently starting or stopping.

Returns
true if the thread is running, false if not.
Version
1.0
bool lunchbox::Thread::isStopped ( ) const

Return if the thread is stopped.

Note that the thread may be neither running nor stopped if it is currently starting or stopping.

Returns
true if the thread is stopped, false if not.
Version
1.0
bool lunchbox::Thread::join ( )

Wait for the exit of the child thread.

Returns
true if the thread was joined, false otherwise.
Version
1.0
virtual void lunchbox::Thread::run ( )
pure virtual

The entry function for the child thread.

This method should contain the main execution routine for the thread and is called after a successful init().

Version
1.0
virtual bool lunchbox::Thread::start ( )
virtual

Start the thread.

Returns
true if the thread was launched and initialized successfully, false otherwise.
See also
init(), run()
Version
1.0

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