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

A monitor primitive. More...

#include <monitor.h>

+ Collaboration diagram for lunchbox::Monitor< T >:

Public Member Functions

 Monitor ()
 Construct a new monitor with a default value of 0. More...
 
 Monitor (const T &value)
 Construct a new monitor with a given default value. More...
 
 Monitor (const Monitor< T > &from)
 Ctor initializing with the given monitor value. More...
 
 ~Monitor ()
 Destructs the monitor. More...
 
template<>
Monitor< bool > & operator++ ()
 
template<>
Monitor< bool > & operator-- ()
 
template<>
Monitor< bool > & operator|= (const bool &value)
 
Changing the monitored value.
Monitoroperator++ ()
 Increment the monitored value, prefix only. More...
 
Monitoroperator-- ()
 Decrement the monitored value, prefix only. More...
 
Monitoroperator= (const T &value)
 Assign a new value. More...
 
const Monitoroperator= (const Monitor< T > &from)
 Assign a new value. More...
 
Monitoroperator|= (const T &value)
 Perform an or operation on the value. More...
 
Monitoroperator&= (const T &value)
 Perform an and operation on the value. More...
 
set (const T &value)
 Set a new value. More...
 
Monitor the value.
const T waitEQ (const T &value) const
 Block until the monitor has the given value. More...
 
const T waitNE (const T &value) const
 Block until the monitor has not the given value. More...
 
const T waitNE (const T &v1, const T &v2) const
 Block until the monitor has none of the given values. More...
 
const T waitGE (const T &value) const
 Block until the monitor has a value greater or equal to the given value. More...
 
const T waitLE (const T &value) const
 Block until the monitor has a value less or equal to the given value. More...
 
const T waitGT (const T &value) const
 Block until the monitor has a value greater than the given value. More...
 
const T waitLT (const T &value) const
 Block until the monitor has a value less than the given value. More...
 
Monitor the value with a timeout.
bool timedWaitEQ (const T &value, const uint32_t timeout) const
 Block until the monitor has the given value. More...
 
bool timedWaitNE (const T &value, const uint32_t timeout) const
 Block until the monitor has not the given value. More...
 
bool timedWaitGE (const T &value, const uint32_t timeout) const
 Block until the monitor has a value greater or equal to the given value. More...
 
bool timedWaitLE (const T &value, const uint32_t timeout) const
 Block until the monitor has a value less or equal to the given value. More...
 
bool timedWaitGT (const T &value, const uint32_t timeout) const
 Block until the monitor has a value greater than the given value. More...
 
bool timedWaitLT (const T &value, const uint32_t timeout) const
 Block until the monitor has a value less than the given value. More...
 
Comparison Operators. @version 1.0
bool operator== (const T &value) const
 
bool operator!= (const T &value) const
 
bool operator< (const T &value) const
 
bool operator> (const T &value) const
 
bool operator<= (const T &value) const
 
bool operator>= (const T &value) const
 
bool operator== (const Monitor< T > &rhs) const
 
bool operator!= (const Monitor< T > &rhs) const
 
bool operator< (const Monitor< T > &rhs) const
 
bool operator> (const Monitor< T > &rhs) const
 
bool operator<= (const Monitor< T > &rhs) const
 
bool operator>= (const Monitor< T > &rhs) const
 
 operator bool_t ()
 
Data Access.
const T & operator-> () const
 
const T & get () const
 
operator+ (const T &value) const
 
operator| (const T &value) const
 
operator& (const T &value) const
 

Detailed Description

template<class T>
class lunchbox::Monitor< T >

A monitor primitive.

A monitor has a value, which can be monitored to reach a certain state. The caller is blocked until the condition is fulfilled. The concept is similar to a pthread condition, with more usage convenience.

Example:

/* Copyright (c) 2010-2012, 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.
*/
#define TEST_RUNTIME 300 // seconds
#include <lunchbox/test.h>
#include <iostream>
#include <lunchbox/clock.h>
#include <lunchbox/monitor.h>
#include <lunchbox/thread.h>
using servus::uint128_t;
class Thread1 : public lunchbox::Thread
{
public:
explicit Thread1(int64_t loops)
: _loops(loops)
{
}
virtual ~Thread1() {}
virtual void run()
{
int64_t nOps = _loops;
while (nOps--)
{
monitor.waitEQ(nOps);
monitor = -nOps;
}
const float time = clock.getTimef();
std::cout << 2 * _loops / time << " ops/ms" << std::endl;
}
private:
int64_t _loops;
};
void testSimpleMonitor()
{
TEST(!boolMonitor);
boolMonitor = true;
TEST(boolMonitor);
const int64_t loops = 200000;
int64_t nOps = loops;
Thread1 waiter(nOps);
TEST(waiter.start());
while (nOps--)
{
monitor = nOps;
monitor.waitEQ(-nOps);
}
const float time = clock.getTimef();
TEST(waiter.join());
std::cout << 2 * loops / time << " ops/ms" << std::endl;
}
class Thread2 : public lunchbox::Thread
{
public:
explicit Thread2(size_t loops)
: innerLoops(0)
, _outerLoops(loops)
{
}
virtual ~Thread2() {}
virtual void run()
{
size_t ops = 0;
for (size_t k = 0; k != 2; ++k)
{
for (size_t i = 0; i != _outerLoops; ++i)
{
const int64_t loops = innerLoops.waitNE(0);
innerLoops = 0;
for (int64_t j = 0; j != loops; ++j)
{
if (innerLoops > 0)
abort();
++monitor;
}
ops += loops * 2 + 2;
}
}
for (size_t k = 0; k != 2; ++k)
{
for (size_t i = 0; i != _outerLoops; ++i)
{
const int64_t loops = innerLoops.waitNE(0);
innerLoops = 0;
for (int64_t j = 0; j != loops; ++j)
{
if (innerLoops > 0)
abort();
--monitor;
}
ops += loops * 2 + 2;
}
}
const float time = clock.getTimef();
std::cout << ops / time << " ops/ms" << std::endl;
}
private:
size_t _outerLoops;
};
void testMonitorComparisons()
{
boolMonitor = true;
const int64_t loops = 1000;
Thread2 waiter(loops);
TEST(waiter.start());
for (int64_t i = 0; i != loops; ++i)
{
monitor = 0;
waiter.innerLoops = i + 1;
TEST(monitor.waitGE(i + 1) >= i + 1);
TEST(monitor >= i + 1);
}
for (int64_t i = 0; i != loops; ++i)
{
monitor = 0;
waiter.innerLoops = i + 1;
TEST(monitor.waitGT(i) > i);
TEST(monitor > i);
}
for (int64_t i = 0; i != loops; ++i)
{
monitor = i + 1;
waiter.innerLoops = i + 1;
TEST(monitor.waitLE(0) <= 0);
TEST(monitor <= 0);
}
for (int64_t i = 0; i != loops; ++i)
{
monitor = i + 1;
waiter.innerLoops = i + 1;
TEST(monitor.waitLT(1) < 1);
TEST(monitor < 1);
}
TEST(waiter.join());
}
void testTimedMonitorComparisons()
{
boolMonitor = true;
const size_t loops = 1000;
Thread2 waiter(loops);
TEST(waiter.start());
const uint32_t timeout = 1;
for (size_t i = 0; i != loops; ++i)
{
monitor = 0;
waiter.innerLoops = i + 1;
while (!monitor.timedWaitGE(i + 1, timeout))
;
TEST(monitor >= i + 1);
}
for (size_t i = 0; i != loops; ++i)
{
monitor = 0;
waiter.innerLoops = i + 1;
while (!monitor.timedWaitGT(i, timeout))
;
TEST(monitor > i);
}
for (size_t i = 0; i != loops; ++i)
{
monitor = i + 1;
waiter.innerLoops = i + 1;
while (!monitor.timedWaitLE(0, timeout))
;
TEST(monitor <= 0);
}
for (size_t i = 0; i != loops; ++i)
{
monitor = i + 1;
waiter.innerLoops = i + 1;
while (!monitor.timedWaitLT(1, timeout))
;
TEST(monitor < 1);
}
TEST(waiter.join());
}
int main(int, char**)
{
testSimpleMonitor();
testMonitorComparisons();
testTimedMonitorComparisons();
return EXIT_SUCCESS;
}

Definition at line 46 of file monitor.h.

Constructor & Destructor Documentation

template<class T>
lunchbox::Monitor< T >::Monitor ( )
inline

Construct a new monitor with a default value of 0.

Version
1.0

Definition at line 52 of file monitor.h.

Referenced by lunchbox::operator<<().

+ Here is the caller graph for this function:

template<class T>
lunchbox::Monitor< T >::Monitor ( const T &  value)
inlineexplicit

Construct a new monitor with a given default value.

Version
1.0

Definition at line 58 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::Monitor ( const Monitor< T > &  from)
inline

Ctor initializing with the given monitor value.

Version
1.1.5

Definition at line 64 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::~Monitor ( )
inline

Destructs the monitor.

Version
1.0

Definition at line 70 of file monitor.h.

Member Function Documentation

template<class T>
const T& lunchbox::Monitor< T >::get ( ) const
inline
Returns
the current value.
Version
1.0

Definition at line 360 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::operator bool_t ( )
inline
Returns
a bool conversion of the result.
Version
1.9.1

Definition at line 348 of file monitor.h.

template<class T>
T lunchbox::Monitor< T >::operator& ( const T &  value) const
inline
Returns
the current and the given value.
Version
1.0

Definition at line 376 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator&= ( const T &  value)
inline

Perform an and operation on the value.

Version
1.7

Definition at line 115 of file monitor.h.

template<class T>
T lunchbox::Monitor< T >::operator+ ( const T &  value) const
inline
Returns
the current plus the given value.
Version
1.0

Definition at line 362 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator++ ( void  )
inline

Increment the monitored value, prefix only.

Version
1.0

Definition at line 74 of file monitor.h.

Referenced by lunchbox::operator<<().

+ Here is the caller graph for this function:

template<class T>
Monitor& lunchbox::Monitor< T >::operator-- ( void  )
inline

Decrement the monitored value, prefix only.

Version
1.0

Definition at line 83 of file monitor.h.

Referenced by lunchbox::operator<<().

+ Here is the caller graph for this function:

template<class T>
const T& lunchbox::Monitor< T >::operator-> ( ) const
inline
Returns
the current value.
Version
1.0

Definition at line 358 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator= ( const T &  value)
inline

Assign a new value.

Version
1.0

Definition at line 92 of file monitor.h.

template<class T>
const Monitor& lunchbox::Monitor< T >::operator= ( const Monitor< T > &  from)
inline

Assign a new value.

Version
1.1.5

Definition at line 99 of file monitor.h.

template<class T>
T lunchbox::Monitor< T >::operator| ( const T &  value) const
inline
Returns
the current or'ed with the given value.
Version
1.0

Definition at line 369 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator|= ( const T &  value)
inline

Perform an or operation on the value.

Version
1.0

Definition at line 106 of file monitor.h.

Referenced by lunchbox::operator<<().

+ Here is the caller graph for this function:

template<class T>
T lunchbox::Monitor< T >::set ( const T &  value)
inline

Set a new value.

Returns
the old value
Version
1.0

Definition at line 124 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitEQ ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.1

Definition at line 217 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitGE ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has a value greater or equal to the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.1

Definition at line 241 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitGT ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has a value greater than the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.10

Definition at line 265 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitLE ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has a value less or equal to the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.10

Definition at line 253 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitLT ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has a value less than the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.10

Definition at line 277 of file monitor.h.

template<class T>
bool lunchbox::Monitor< T >::timedWaitNE ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has not the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for not the value.
Returns
true on success, false on timeout.
Version
1.10

Definition at line 229 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitEQ ( const T &  value) const
inline

Block until the monitor has the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 141 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitGE ( const T &  value) const
inline

Block until the monitor has a value greater or equal to the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 173 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitGT ( const T &  value) const
inline

Block until the monitor has a value greater than the given value.

Returns
the value when reaching the condition.
Version
1.10

Definition at line 193 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitLE ( const T &  value) const
inline

Block until the monitor has a value less or equal to the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 183 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitLT ( const T &  value) const
inline

Block until the monitor has a value less than the given value.

Returns
the value when reaching the condition.
Version
1.10

Definition at line 203 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitNE ( const T &  value) const
inline

Block until the monitor has not the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 151 of file monitor.h.

template<class T>
const T lunchbox::Monitor< T >::waitNE ( const T &  v1,
const T &  v2 
) const
inline

Block until the monitor has none of the given values.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 161 of file monitor.h.


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