diff options
author | David Robillard <d@drobilla.net> | 2012-05-12 15:49:11 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-05-12 15:49:11 +0000 |
commit | 6eb06945e4115f5f7c67368dfb05646e8ee72caa (patch) | |
tree | 82e178fab4c51c4bf044bc999e00bb74d043dec2 /test/sem_test.cpp | |
parent | 73a7dd2818c8eb2d46b2065347030691bfc8140a (diff) | |
download | raul-6eb06945e4115f5f7c67368dfb05646e8ee72caa.tar.gz raul-6eb06945e4115f5f7c67368dfb05646e8ee72caa.tar.bz2 raul-6eb06945e4115f5f7c67368dfb05646e8ee72caa.zip |
Add Semaphore::timed_wait().
git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4379 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'test/sem_test.cpp')
-rw-r--r-- | test/sem_test.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/sem_test.cpp b/test/sem_test.cpp new file mode 100644 index 0000000..e1f2fb4 --- /dev/null +++ b/test/sem_test.cpp @@ -0,0 +1,49 @@ +#include <iostream> +#include "raul/Thread.hpp" +#include "raul/Semaphore.hpp" + +using namespace std; +using namespace Raul; + +class Waiter : public Raul::Thread { +public: + Waiter(Semaphore& sem) : _sem(sem) { + Thread::set_name("Waiter"); + } + +private: + void _run() { + while (true) { + if (_sem.timed_wait(250)) { + cout << "[Waiter] Received signal" << endl; + break; + } else { + cout << "[Waiter] Timed out" << endl; + } + } + cout << "[Waiter] Exiting" << endl; + } + + Semaphore& _sem; +}; + +int +main() +{ + Thread& this_thread = Thread::get(); + this_thread.set_name("Main"); + + Semaphore sem(0); + Waiter waiter(sem); + waiter.start(); + + sleep(1); + + cout << "[Main] Signalling..." << endl; + sem.post(); + + waiter.join(); + cout << "[Main] Exiting" << endl; + + return 0; +} |