thread.test.h
Go to the documentation of this file.
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
(c) 2007 Enrico Zini <enrico@enricozini.org> */
#include <wibble/sys/mutex.h>
#include <wibble/sys/thread.h>
#include <wibble/test.h>
using namespace std;
using namespace wibble::sys;
struct TestThread {
// Test threads that just assigns a value to an int and exists
class Thread1 : public Thread
{
protected:
int& res;
int val;
void* main()
{
res = val;
return reinterpret_cast<void*>(val);
}
public:
Thread1(int& res, int val) : res(res), val(val) {}
};
// Thread that continuously increments an int value
class Thread2 : public Thread
{
protected:
int& res;
Mutex& mutex;
bool done;
void* main()
{
while (!done)
{
MutexLock lock(mutex);
++res;
}
return 0;
}
public:
Thread2(int& res, Mutex& mutex) :
res(res), mutex(mutex), done(false) {}
void quit() { done = true; }
};
// Test that threads are executed
Test execution() {
int val = 0;
Thread1 assigner(val, 42);
assigner.start();
assert_eq(assigner.join(), reinterpret_cast<void*>(42));
assert_eq(val, 42);
}
// Use mutexes to access shared memory
Test sharedMemory() {
int val = 0;
Mutex mutex;
Thread2 incrementer(val, mutex);
incrementer.start();
bool done = false;
while (!done)
{
MutexLock lock(mutex);
if (val > 100)
done = true;
}
incrementer.quit();
assert_eq(incrementer.join(), static_cast<void*>(0));
}
};
// vim:set ts=4 sw=4:
TestThread::Thread1::res
int & res
Definition: thread.test.h:18
TestThread::Thread1::Thread1
Thread1(int &res, int val)
Definition: thread.test.h:27
TestThread::Thread2::quit
void quit()
Definition: thread.test.h:51
TestThread::Thread2::Thread2
Thread2(int &res, Mutex &mutex)
Definition: thread.test.h:49
TestThread
Definition: thread.test.h:12
Test
void Test
Definition: test.h:178
wibble::sys::Thread::start
void start()
Start the thread.
Definition: thread.cpp:70
TestThread::Thread1::main
void * main()
Main thread function, executed in the new thread after creation.
Definition: thread.test.h:21
test.h
std
wibble::sys
Definition: buffer.cpp:28
TestThread::Thread2::done
bool done
Definition: thread.test.h:36
wibble::sys::MutexLockT
Acquire a mutex lock, RAII-style.
Definition: mutex.h:200
TestThread::Thread2::mutex
Mutex & mutex
Definition: thread.test.h:35
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
TestThread::Thread2::res
int & res
Definition: thread.test.h:34
thread.h
wibble::sys::Thread
Encapsulates a thread.
Definition: thread.h:83
wibble::sys::Mutex
pthread mutex wrapper; WARNING: the class allows copying and assignment, but this is not always safe...
Definition: mutex.h:47
TestThread::Thread2::main
void * main()
Main thread function, executed in the new thread after creation.
Definition: thread.test.h:38
TestThread::Thread1
Definition: thread.test.h:15
TestThread::Thread1::val
int val
Definition: thread.test.h:19
wibble::sys::Thread::join
void * join()
Join the thread.
Definition: thread.cpp:100
TestThread::sharedMemory
Test sharedMemory()
Definition: thread.test.h:65
mutex.h
TestThread::Thread2
Definition: thread.test.h:31
TestThread::execution
Test execution()
Definition: thread.test.h:55