source: vscpl/test/testmutex.cc@ 17:01bf0349a50e

Last change on this file since 17:01bf0349a50e was 4:f5d22f58ef6b, checked in by István Váradi <ivaradi@…>, 12 years ago

Added the Mutex implementations

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