// Copyright (c) 2012 by István Váradi // This file is part of libxplcommon, a common utility library for // development related to X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include #include #include #include #include #include #include #include //------------------------------------------------------------------------------ using hu::varadiistvan::scpl::Thread; using hu::varadiistvan::scpl::Mutex; using hu::varadiistvan::scpl::CondVar; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ class Thread1 : public Thread { private: char id; Mutex& mutex; CondVar& condVar; public: Thread1(char id, Mutex& mutex, CondVar& condVar); virtual void run(); }; //------------------------------------------------------------------------------ inline Thread1::Thread1(char id, Mutex& mutex, CondVar& condVar) : id(id), mutex(mutex), condVar(condVar) { } //------------------------------------------------------------------------------ void Thread1::run() { printf("Thread1[%c]::run: locking, and waiting on the condition\n", id); mutex.lock(); condVar.wait(mutex); printf("Thread1[%c]::run: condition received, waiting again\n", id); condVar.wait(mutex); printf("Thread1[%c]::run: condition received again, waiting with timeout\n", id); bool result = condVar.wait(mutex, 5000); printf("Thread1[%c]::run: waiting finished, %s\n", id, result ? "received condition" : "timed out"); mutex.unlock(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ class Thread2 : public Thread { private: Mutex& mutex; CondVar& condVar; public: Thread2(Mutex& mutex, CondVar& condVar); virtual void run(); }; //------------------------------------------------------------------------------ inline Thread2::Thread2(Mutex& mutex, CondVar& condVar) : mutex(mutex), condVar(condVar) { } //------------------------------------------------------------------------------ void Thread2::run() { printf("Thread2::run: sleeping\n"); sleep(2000); printf("------------------------------------------------------------\n"); printf("Thread2::run: woke up, signalling condition variable\n"); condVar.signal(); printf("Thread2::run: sleeping...\n"); sleep(2000); printf("------------------------------------------------------------\n"); printf("Thread2::run: woke up, broadcasting condition variable\n"); condVar.broadcast(); printf("Thread2::run: sleeping...\n"); sleep(2000); printf("------------------------------------------------------------\n"); printf("Thread2::run: woke up, signalling condition variable\n"); condVar.signal(); printf("Thread2::run: sleeping...\n"); sleep(2000); printf("------------------------------------------------------------\n"); printf("Thread2::run: woke up, signalling condition variable\n"); condVar.signal(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ int main() { Mutex mutex; CondVar condVar; Thread1 thread1('A', mutex, condVar); Thread1 thread1a('B', mutex, condVar); Thread2 thread2(mutex, condVar); printf("Starting threads\n"); thread1.start(); thread1a.start(); thread2.start(); printf("Waiting for thread1\n"); thread1.join(); thread1a.join(); printf("Waiting for thread2\n"); thread2.join(); printf("Both threads returned\n"); return 0; } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: