[34] | 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/Mutex.h>
|
---|
| 34 | #include <xplcommon/CondVar.h>
|
---|
| 35 |
|
---|
| 36 | #include <cstdlib>
|
---|
| 37 | #include <cstdio>
|
---|
| 38 | #include <cassert>
|
---|
| 39 |
|
---|
| 40 | #include <signal.h>
|
---|
| 41 |
|
---|
| 42 | #include <sys/time.h>
|
---|
| 43 |
|
---|
| 44 | //------------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | using xplcommon::Thread;
|
---|
| 47 | using xplcommon::Mutex;
|
---|
| 48 | using xplcommon::CondVar;
|
---|
| 49 |
|
---|
| 50 | //------------------------------------------------------------------------------
|
---|
| 51 | //------------------------------------------------------------------------------
|
---|
| 52 |
|
---|
| 53 | class Thread1 : public Thread
|
---|
| 54 | {
|
---|
| 55 | private:
|
---|
| 56 | char id;
|
---|
| 57 |
|
---|
| 58 | Mutex& mutex;
|
---|
| 59 |
|
---|
| 60 | CondVar& condVar;
|
---|
| 61 |
|
---|
| 62 | public:
|
---|
| 63 | Thread1(char id, Mutex& mutex, CondVar& condVar);
|
---|
| 64 |
|
---|
| 65 | virtual void run();
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | //------------------------------------------------------------------------------
|
---|
| 69 |
|
---|
| 70 | inline Thread1::Thread1(char id, Mutex& mutex, CondVar& condVar) :
|
---|
| 71 | id(id),
|
---|
| 72 | mutex(mutex),
|
---|
| 73 | condVar(condVar)
|
---|
| 74 | {
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | //------------------------------------------------------------------------------
|
---|
| 78 |
|
---|
| 79 | void Thread1::run()
|
---|
| 80 | {
|
---|
| 81 | printf("Thread1[%c]::run: locking, and waiting on the condition\n", id);
|
---|
| 82 | mutex.lock();
|
---|
| 83 | condVar.wait(mutex);
|
---|
| 84 | printf("Thread1[%c]::run: condition received, waiting again\n", id);
|
---|
| 85 | condVar.wait(mutex);
|
---|
| 86 | printf("Thread1[%c]::run: condition received again, waiting with timeout\n", id);
|
---|
| 87 |
|
---|
| 88 | bool result = condVar.wait(mutex, 5000);
|
---|
| 89 | printf("Thread1[%c]::run: waiting finished, %s\n", id,
|
---|
| 90 | result ? "received condition" : "timed out");
|
---|
| 91 | mutex.unlock();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | //------------------------------------------------------------------------------
|
---|
| 95 | //------------------------------------------------------------------------------
|
---|
| 96 |
|
---|
| 97 | class Thread2 : public Thread
|
---|
| 98 | {
|
---|
| 99 | private:
|
---|
| 100 | Mutex& mutex;
|
---|
| 101 |
|
---|
| 102 | CondVar& condVar;
|
---|
| 103 |
|
---|
| 104 | public:
|
---|
| 105 | Thread2(Mutex& mutex, CondVar& condVar);
|
---|
| 106 |
|
---|
| 107 | virtual void run();
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | //------------------------------------------------------------------------------
|
---|
| 111 |
|
---|
| 112 | inline Thread2::Thread2(Mutex& mutex, CondVar& condVar) :
|
---|
| 113 | mutex(mutex),
|
---|
| 114 | condVar(condVar)
|
---|
| 115 | {
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | //------------------------------------------------------------------------------
|
---|
| 119 |
|
---|
| 120 | void Thread2::run()
|
---|
| 121 | {
|
---|
| 122 | printf("Thread2::run: sleeping\n");
|
---|
| 123 | sleep(2000);
|
---|
| 124 | printf("------------------------------------------------------------\n");
|
---|
| 125 |
|
---|
| 126 | printf("Thread2::run: woke up, signalling condition variable\n");
|
---|
| 127 | condVar.signal();
|
---|
| 128 |
|
---|
| 129 | printf("Thread2::run: sleeping...\n");
|
---|
| 130 | sleep(2000);
|
---|
| 131 | printf("------------------------------------------------------------\n");
|
---|
| 132 |
|
---|
| 133 | printf("Thread2::run: woke up, broadcasting condition variable\n");
|
---|
| 134 | condVar.broadcast();
|
---|
| 135 |
|
---|
| 136 | printf("Thread2::run: sleeping...\n");
|
---|
| 137 | sleep(2000);
|
---|
| 138 | printf("------------------------------------------------------------\n");
|
---|
| 139 |
|
---|
| 140 | printf("Thread2::run: woke up, signalling condition variable\n");
|
---|
| 141 | condVar.signal();
|
---|
| 142 |
|
---|
| 143 | printf("Thread2::run: sleeping...\n");
|
---|
| 144 | sleep(2000);
|
---|
| 145 | printf("------------------------------------------------------------\n");
|
---|
| 146 |
|
---|
| 147 | printf("Thread2::run: woke up, signalling condition variable\n");
|
---|
| 148 | condVar.signal();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | //------------------------------------------------------------------------------
|
---|
| 152 | //------------------------------------------------------------------------------
|
---|
| 153 |
|
---|
| 154 | int main()
|
---|
| 155 | {
|
---|
| 156 | Mutex mutex;
|
---|
| 157 | CondVar condVar;
|
---|
| 158 |
|
---|
| 159 | Thread1 thread1('A', mutex, condVar);
|
---|
| 160 | Thread1 thread1a('B', mutex, condVar);
|
---|
| 161 | Thread2 thread2(mutex, condVar);
|
---|
| 162 |
|
---|
| 163 | printf("Starting threads\n");
|
---|
| 164 | thread1.start();
|
---|
| 165 | thread1a.start();
|
---|
| 166 | thread2.start();
|
---|
| 167 |
|
---|
| 168 | printf("Waiting for thread1\n");
|
---|
| 169 | thread1.join();
|
---|
| 170 | thread1a.join();
|
---|
| 171 | printf("Waiting for thread2\n");
|
---|
| 172 | thread2.join();
|
---|
| 173 | printf("Both threads returned\n");
|
---|
| 174 |
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | //------------------------------------------------------------------------------
|
---|
| 179 |
|
---|
| 180 | // Local Variables:
|
---|
| 181 | // mode: C++
|
---|
| 182 | // c-basic-offset: 4
|
---|
| 183 | // indent-tabs-mode: nil
|
---|
| 184 | // End:
|
---|