1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
//===- ThreadTest.h -----------------------------------------------------===//
//
// The SkyPat team
//
// This file is distributed under the New BSD License.
// See LICENSE for details.
//
//===----------------------------------------------------------------------===//
#include <skypat/skypat.h>
#include <skypat/Thread/Thread.h>
#include <skypat/Thread/Mutex.h>
using namespace skypat;
class Counter
{
public:
static Counter* self() {
static Counter instance;
return &instance;
}
unsigned int value() const {
ScopedLock locker(m_Mutex);
return m_Value;
}
void increase() {
ScopedLock locker(m_Mutex);
++m_Value;
}
private:
unsigned int m_Value;
mutable Mutex m_Mutex;
};
class WriterThread : public Thread
{
public:
void run() {
while(20 != Counter::self()->value())
Counter::self()->increase();
}
};
class MonitorThread : public Thread
{
public:
void run() {
while (20 != Counter::self()->value())
;
get_ten = true;
}
bool getTen() const { return get_ten; }
public:
static bool get_ten;
};
bool MonitorThread::get_ten = false;
|