source: xplcommon/test/testevent.cc@ 5:db1fa4c31a20

Last change on this file since 5:db1fa4c31a20 was 4:d061ad3be7fd, checked in by István Váradi <ivaradi@…>, 12 years ago

Added support for a waitable event for POSIX and a test program

File size: 4.3 KB
RevLine 
[4]1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30//------------------------------------------------------------------------------
31
32#include <xplcommon/Thread.h>
33#include <xplcommon/Waiter.h>
34#include <xplcommon/WaitableEvent.h>
35
36#include <cstdio>
37
38//------------------------------------------------------------------------------
39
40using xplcommon::Thread;
41using xplcommon::Waiter;
42using xplcommon::WaitableEvent;
43
44//------------------------------------------------------------------------------
45
46class ServerThread : public Thread
47{
48private:
49 Waiter waiter;
50
51 WaitableEvent event;
52
53public:
54 ServerThread();
55
56 WaitableEvent& getEvent();
57
58 virtual void run();
59};
60
61//------------------------------------------------------------------------------
62
63inline ServerThread::ServerThread() :
64 event(&waiter)
65{
66}
67
68//------------------------------------------------------------------------------
69
70inline WaitableEvent& ServerThread::getEvent()
71{
72 return event;
73}
74
75//------------------------------------------------------------------------------
76
77void ServerThread::run()
78{
79 printf("ServerThread::run\n");
80 while(!event.check()) {
81 printf("ServerThread::run: waiting...\n");
82 waiter.wait();
83 printf("ServerThread::run: waiting done\n");
84 }
85 printf("ServerThread::run: finished\n");
86}
87
88//------------------------------------------------------------------------------
89//------------------------------------------------------------------------------
90
91class ClientThread : public Thread
92{
93private:
94 WaitableEvent& event;
95
96public:
97 ClientThread(WaitableEvent& event);
98
99 virtual void run();
100};
101
102//------------------------------------------------------------------------------
103
104inline ClientThread::ClientThread(WaitableEvent& event) :
105 event(event)
106{
107}
108
109//------------------------------------------------------------------------------
110
111void ClientThread::run()
112{
113 printf("ClientThread::run: sleeping\n");
114 sleep(2000);
115 printf("ClientThread::run: firing event\n");
116 event.fire();
117}
118
119//------------------------------------------------------------------------------
120//------------------------------------------------------------------------------
121
122int main()
123{
124 ServerThread serverThread;
125 ClientThread clientThread(serverThread.getEvent());
126
127 printf("Starting threads\n");
128 serverThread.start();
129 clientThread.start();
130
131 printf("Waiting for the client thread\n");
132 clientThread.join();
133 printf("Waiting for the server thread\n");
134 serverThread.join();
135 printf("Both threads returned\n");
136
137 return 0;
138}
139
140//------------------------------------------------------------------------------
141
142// Local Variables:
143// mode: C++
144// c-basic-offset: 4
145// indent-tabs-mode: nil
146// End:
Note: See TracBrowser for help on using the repository browser.