blob: 03445d9b82050c79e7497f4623667fa1a2a502d3 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26
27
28#include "Threads.h"
29#include "Interthread.h"
30#include <iostream>
31
32using namespace std;
33
34
35InterthreadQueue<int> gQ;
36InterthreadMap<int,int> gMap;
37
38void* qWriter(void*)
39{
40 int *p;
41 for (int i=0; i<20; i++) {
42 p = new int;
43 *p = i;
44 COUT("queue write " << *p);
45 gQ.write(p);
46 if (random()%2) sleep(1);
47 }
48 p = new int;
49 *p = -1;
50 gQ.write(p);
51 return NULL;
52}
53
54void* qReader(void*)
55{
56 bool done = false;
57 while (!done) {
58 int *p = gQ.read();
59 COUT("queue read " << *p);
60 if (*p<0) done=true;
61 delete p;
62 }
63 return NULL;
64}
65
66
67void* mapWriter(void*)
68{
69 int *p;
70 for (int i=0; i<20; i++) {
71 p = new int;
72 *p = i;
73 COUT("map write " << *p);
74 gMap.write(i,p);
75 if (random()%2) sleep(1);
76 }
77 return NULL;
78}
79
80void* mapReader(void*)
81{
82 for (int i=0; i<20; i++) {
83 int *p = gMap.read(i);
84 COUT("map read " << *p);
kurtis.heimerl5a872472013-05-31 21:47:25 +000085 // InterthreadMap will delete the pointers
86 // delete p;
dburgess82c46ff2011-10-07 02:40:51 +000087 }
88 return NULL;
89}
90
91
92
93
94
95
96int main(int argc, char *argv[])
97{
98 Thread qReaderThread;
99 qReaderThread.start(qReader,NULL);
100 Thread mapReaderThread;
101 mapReaderThread.start(mapReader,NULL);
102
103 Thread qWriterThread;
104 qWriterThread.start(qWriter,NULL);
105 Thread mapWriterThread;
106 mapWriterThread.start(mapWriter,NULL);
107
108 qReaderThread.join();
109 qWriterThread.join();
110 mapReaderThread.join();
111 mapWriterThread.join();
112}
113
114
115// vim: ts=4 sw=4