File: thread.cxx

package info (click to toggle)
pwlib 1.10.10-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,068 kB
  • ctags: 15,167
  • sloc: cpp: 112,149; ansic: 6,061; sh: 2,920; makefile: 1,062; yacc: 861; asm: 161
file content (212 lines) | stat: -rw-r--r-- 5,508 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * thread.cxx
 *
 * Sample program to test PWLib threads.
 *
 * Portable Windows Library
 *
 * Copyright (c) 2001,2002 Roger Hardiman
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Portable Windows Library.
 *
 * The Initial Developer of the Original Code is Roger Hardiman
 *
 * $Log: thread.cxx,v $
 * Revision 1.6  2003/01/07 10:04:13  rogerh
 * Revert to 2 seconds per phase
 *
 * Revision 1.5  2002/11/04 22:46:23  rogerh
 * Implement a Stop() method to make the threads terminate.
 *
 * Revision 1.4  2002/11/04 18:11:22  rogerh
 * Terminate the threads prior to deletion.
 *
 * Revision 1.3  2002/11/04 16:24:21  rogerh
 * Delete the threads, fixing a memory leak.
 *
 * Revision 1.2  2001/09/27 08:22:48  rogerh
 * Doing a flush on cout does not work on Mac OS X. So you do not see any
 * results until the program stops. So replace the printing of the numbers with
 * good old printf and fflush.
 *
 * Revision 1.1  2001/09/21 09:18:28  rogerh
 * Add a thread test program which demonstrates thread, suspend and resume.
 *
 *
 */

/*
 * This sample program tests threads is PWLib. It creates two threads,
 * one which display the number '1' and one which displays the number '2'.
 * It also demonstrates starting a thread with Resume(), using
 * Suspend() and Resume() to suspend a running thread and two different
 * ways to make a thread terminate.
 */

#include <ptlib.h>

/*
 * Thread #1 displays the number 1 every 10ms.
 * When it is created, Main() starts executing immediatly.
 * The thread is terminated by calling Stop() which uses a PSyncPoint with a
 * 10ms timeout.
 */
class MyThread1 : public PThread
{
  PCLASSINFO(MyThread1, PThread);
  public:
    MyThread1() : PThread(1000,NoAutoDeleteThread)
    {
      Resume(); // start running this thread when it is created.
    }

    void Main() {
      while (!shutdown.Wait(10)) { // 10ms delay
        printf("1 ");
        fflush(stdout);
	Sleep(10);
      }
    }

    void Stop() {
      // signal the shutdown PSyncPoint. On the next iteration, the thread's
      // Main() function will exit cleanly.
      shutdown.Signal();
    }

    protected:
      PSyncPoint shutdown;
};


/*
 * Thread #2 displays the number 2 every 10 ms.
 * This thread will not start automatically. We must call
 * Resume() after creating the thread.
 * The thread is terminated by calling Stop() which sets a local variable.
 */
class MyThread2 : public PThread
{
  PCLASSINFO(MyThread2, PThread);
  public:
    MyThread2() : PThread(1000,NoAutoDeleteThread) {
      exitFlag = FALSE;
    }

    void Main() {
      while (1) {
        // Check if we need to exit
        exitMutex.Wait();
        if (exitFlag == TRUE) {
          exitMutex.Signal();
          break;
        }
        exitMutex.Signal();

        // Display the number 2, then sleep for a short time
        printf("2 "); fflush(stdout);
	Sleep(10); // sleep 10ms
      }
    }

    void Stop() {
      // set the exit flag. On the next iteration, the thread's
      // Main() function will exit cleanly.
      exitMutex.Wait();
      exitFlag = TRUE;
      exitMutex.Signal();
    }

    protected:
      PMutex exitMutex;
      BOOL exitFlag;
};


/*
 * The main program class
 */
class ThreadTest : public PProcess
{
  PCLASSINFO(ThreadTest, PProcess)
  public:
    void Main();
};

PCREATE_PROCESS(ThreadTest);

// The main program
void ThreadTest::Main()
{
  cout << "Thread Test Program" << endl;
  cout << "This program will display the following:" << endl;
  cout << "             2 seconds of 1 1 1 1 1..." << endl;
  cout << " followed by 2 seconds of 1 2 1 2 1 2 1 2 1 2..." << endl;
  cout << " followed by 2 seconds of 2 2 2 2 2..." << endl;
  cout << " followed by 2 seconds of 1 2 1 2 1 2 1 2 1 2..." << endl;
  cout << endl;
  cout << "It tests thread creation, suspend and resume functions." << endl;
  cout << endl;

  // Create the threads
  MyThread1 * mythread1;
  MyThread2 * mythread2;

  mythread1 = new MyThread1();
  mythread2 = new MyThread2();


  // Thread 1 should now be running, as there is a Resume() function
  // in the thread constructor.
  // Thread 2 should be suspended.
  // Sleep for three seconds. Only thread 1 will be running.
  // Display will show "1 1 1 1 1 1 1..."
  sleep(2);


  // Start the second thread.
  // Both threads should be running
  // Sleep for 3 seconds, allowing the threads to run.
  // Display will show "1 2 1 2 1 2 1 2 1 2..."
  mythread2->Resume();
  sleep(2);


  // Suspend thread 1.
  // Sleep for 3 seconds. Only thread 2 should be running.
  // Display will show "2 2 2 2 2 2 2..."
  mythread1->Suspend();
  sleep(2);


  // Resume thread 1.
  // Sleep for 3 seconds. Both threads should be running.
  // Display will show "1 2 1 2 1 2 1 2 1 2..."
  mythread1->Resume();
  sleep(2);


  // Clean up
  mythread1->Stop();
  mythread1->WaitForTermination();
  cout << "Thread 1 terminated" << endl;

  mythread2->Stop();
  mythread2->WaitForTermination();
  cout << "Thread 2 terminated" << endl;

  delete mythread1;
  delete mythread2;

}