blob: c131c2a8125855ef582b96d2674a60c521cd6be6 [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
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010038int q_last_read_val = -1;
39int q_last_write_val;
40int m_last_read_val;
41int m_last_write_val;
42
dburgess82c46ff2011-10-07 02:40:51 +000043void* qWriter(void*)
44{
45 int *p;
46 for (int i=0; i<20; i++) {
47 p = new int;
48 *p = i;
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010049 CERR("queue write " << *p);
dburgess82c46ff2011-10-07 02:40:51 +000050 gQ.write(p);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010051 q_last_write_val = i;
dburgess82c46ff2011-10-07 02:40:51 +000052 if (random()%2) sleep(1);
53 }
54 p = new int;
55 *p = -1;
56 gQ.write(p);
57 return NULL;
58}
59
60void* qReader(void*)
61{
62 bool done = false;
63 while (!done) {
64 int *p = gQ.read();
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010065 CERR("queue read " << *p);
66 if (*p<0) {
67 assert(q_last_read_val == 19 && *p == -1);
68 done = true;
69 } else {
70 assert(q_last_read_val == *p - 1);
71 q_last_read_val = *p;
72 }
dburgess82c46ff2011-10-07 02:40:51 +000073 delete p;
74 }
75 return NULL;
76}
77
78
79void* mapWriter(void*)
80{
81 int *p;
82 for (int i=0; i<20; i++) {
83 p = new int;
84 *p = i;
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010085 CERR("map write " << *p);
dburgess82c46ff2011-10-07 02:40:51 +000086 gMap.write(i,p);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010087 m_last_write_val = i;
dburgess82c46ff2011-10-07 02:40:51 +000088 if (random()%2) sleep(1);
89 }
90 return NULL;
91}
92
93void* mapReader(void*)
94{
95 for (int i=0; i<20; i++) {
96 int *p = gMap.read(i);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010097 CERR("map read " << *p);
98 assert(*p == i);
99 m_last_read_val = *p;
kurtis.heimerl5a872472013-05-31 21:47:25 +0000100 // InterthreadMap will delete the pointers
101 // delete p;
dburgess82c46ff2011-10-07 02:40:51 +0000102 }
103 return NULL;
104}
105
106
107
108
109
110
111int main(int argc, char *argv[])
112{
113 Thread qReaderThread;
114 qReaderThread.start(qReader,NULL);
115 Thread mapReaderThread;
116 mapReaderThread.start(mapReader,NULL);
117
118 Thread qWriterThread;
119 qWriterThread.start(qWriter,NULL);
120 Thread mapWriterThread;
121 mapWriterThread.start(mapWriter,NULL);
122
123 qReaderThread.join();
124 qWriterThread.join();
125 mapReaderThread.join();
126 mapWriterThread.join();
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +0100127
128 assert(q_last_write_val == 19);
129 assert(q_last_read_val == 19);
130 assert(m_last_write_val == 19);
131 assert(m_last_read_val == 19);
132
133 printf("Done\n");
dburgess82c46ff2011-10-07 02:40:51 +0000134}
135
136
137// vim: ts=4 sw=4