Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::PerThread< T, D > Class Template Reference

Implements thread-specific storage for C++ objects. More...

#include <perThread.h>

+ Collaboration diagram for lunchbox::PerThread< T, D >:

Public Member Functions

 PerThread ()
 Construct a new per-thread variable. More...
 
 ~PerThread ()
 Destruct the per-thread variable. More...
 
PerThread< T, D > & operator= (const T *data)
 Assign an object to the thread-local storage. More...
 
PerThread< T, D > & operator= (const PerThread< T, D > &rhs)
 Assign an object from another thread-local storage. More...
 
T * get ()
 
const T * get () const
 
T * operator-> ()
 Access the thread-local object. More...
 
const T * operator-> () const
 Access the thread-local object. More...
 
T & operator* ()
 
const T & operator* () const
 
bool operator== (const PerThread &rhs) const
 
bool operator== (const T *rhs) const
 
bool operator!= (const T *rhs) const
 
bool operator! () const
 
bool isValid () const
 

Detailed Description

template<class T, void(*)(T *) D = &perThreadDelete<T>>
class lunchbox::PerThread< T, D >

Implements thread-specific storage for C++ objects.

The default destructor function deletes the object on thread exit.

Parameters
Tthe type of data to store in thread-local storage
Dthe destructor callback function.
Deprecated:
Use boost::thread_specific_ptr

Example:

/* Copyright (c) 2011-2014, Stefan Eilemann <eile@eyescale.ch>
*
* 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 <lunchbox/test.h>
#include <pthread.h>
#include <lunchbox/atomic.h>
#include <lunchbox/perThread.h>
#include <lunchbox/rng.h>
#include <lunchbox/sleep.h>
#define NTHREADS 1024
void destructor(size_t*)
{
--_threads;
}
class Thread : public lunchbox::Thread
{
public:
virtual ~Thread() {}
virtual void run()
{
++_threads;
size_t i = rng.get<size_t>();
size_t* data = new size_t(i);
_tls1 = data;
_tls2 = data;
_tls3 = _tls1;
TEST(_tls1.get() == data);
TEST(*_tls1 == i);
TEST(_tls2.get() == data);
TEST(*_tls2 == i);
TEST(*_tls1 == *_tls2);
TEST(*_tls1 == *_tls3);
_tls3 = 0;
TEST(_tls3 == 0);
}
};
int main(int argc, char** argv)
{
TEST(lunchbox::init(argc, argv));
Thread threads[NTHREADS];
TESTINFO(_threads == 0, _threads);
for (size_t i = 0; i < NTHREADS; ++i)
TEST(threads[i].start());
for (size_t i = 0; i < NTHREADS; ++i)
TEST(threads[i].join());
lunchbox::sleep(10); // ms, needed for tls exit handlers to run
TESTINFO(_threads == 0, _threads);
TEST(lunchbox::exit());
return EXIT_SUCCESS;
}

Definition at line 57 of file perThread.h.

Constructor & Destructor Documentation

template<class T , void(*)(T *) D>
lunchbox::PerThread< T, D >::PerThread ( )

Construct a new per-thread variable.

Version
1.0

Definition at line 21 of file perThread.ipp.

template<class T , void(*)(T *) D>
lunchbox::PerThread< T, D >::~PerThread ( )

Destruct the per-thread variable.

Version
1.0

Definition at line 27 of file perThread.ipp.

Member Function Documentation

template<class T , void(*)(T *) D>
T * lunchbox::PerThread< T, D >::get ( )
Returns
the held object pointer.
Version
1.0

Definition at line 46 of file perThread.ipp.

References lunchbox::TLS::get().

Referenced by lunchbox::PerThread< T, D >::operator==().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T , void(*)(T *) D>
const T * lunchbox::PerThread< T, D >::get ( ) const
Returns
the held object pointer.
Version
1.0

Definition at line 52 of file perThread.ipp.

References lunchbox::TLS::get().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
bool lunchbox::PerThread< T, D >::isValid ( ) const
Returns
true if the thread-local storage holds a non-0 pointer.
Version
1.0

Definition at line 76 of file perThread.ipp.

References lunchbox::TLS::get().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
bool lunchbox::PerThread< T, D >::operator! ( ) const
Returns
true if the thread-local storage holds a 0 pointer.
Version
1.0

Definition at line 70 of file perThread.ipp.

References lunchbox::TLS::get().

+ Here is the call graph for this function:

template<class T, void(*)(T *) D = &perThreadDelete<T>>
bool lunchbox::PerThread< T, D >::operator!= ( const T *  rhs) const
inline
Returns
true if the thread-local variable holds another object.
Version
1.0

Definition at line 106 of file perThread.h.

template<class T, void(*)(T *) D = &perThreadDelete<T>>
T& lunchbox::PerThread< T, D >::operator* ( )
inline
Returns
the held object reference.
Version
1.0

Definition at line 80 of file perThread.h.

References lunchbox::className().

+ Here is the call graph for this function:

template<class T, void(*)(T *) D = &perThreadDelete<T>>
const T& lunchbox::PerThread< T, D >::operator* ( ) const
inline
Returns
the held object reference.
Version
1.0

Definition at line 86 of file perThread.h.

References lunchbox::className().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
T * lunchbox::PerThread< T, D >::operator-> ( )

Access the thread-local object.

Version
1.0

Definition at line 58 of file perThread.ipp.

References lunchbox::TLS::get().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
const T * lunchbox::PerThread< T, D >::operator-> ( ) const

Access the thread-local object.

Version
1.0

Definition at line 64 of file perThread.ipp.

References lunchbox::TLS::get().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
PerThread< T, D > & lunchbox::PerThread< T, D >::operator= ( const T *  data)

Assign an object to the thread-local storage.

Version
1.0

Definition at line 32 of file perThread.ipp.

References lunchbox::TLS::set().

+ Here is the call graph for this function:

template<class T , void(*)(T *) D>
PerThread< T, D > & lunchbox::PerThread< T, D >::operator= ( const PerThread< T, D > &  rhs)

Assign an object from another thread-local storage.

Version
1.0

Definition at line 39 of file perThread.ipp.

References lunchbox::TLS::set().

+ Here is the call graph for this function:

template<class T, void(*)(T *) D = &perThreadDelete<T>>
bool lunchbox::PerThread< T, D >::operator== ( const PerThread< T, D > &  rhs) const
inline
Returns
true if the thread-local variables hold the same object.
Version
1.0

Definition at line 96 of file perThread.h.

References lunchbox::PerThread< T, D >::get().

+ Here is the call graph for this function:

template<class T, void(*)(T *) D = &perThreadDelete<T>>
bool lunchbox::PerThread< T, D >::operator== ( const T *  rhs) const
inline
Returns
true if the thread-local variable holds the same object.
Version
1.0

Definition at line 101 of file perThread.h.


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