Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test-con-queue.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <iostream>
34# include <cerrno>
35# include <chrono>
36# include <thread>
37# include <q-consumer-threads.H>
38
39using namespace std;
40
41static constexpr size_t WORK_DELAY_MS = 10;
42
43struct Event1 : QueueTheadsPool<int>::Event
44{
45 void run() override
46 {
47 // cout << "Event 1 " << item << endl;
48 this_thread::sleep_for(chrono::milliseconds(WORK_DELAY_MS));
50 }
51};
52
53struct Event2 : QueueTheadsPool<int>::Event
54{
55 void run() override
56 {
57 // cout << "Event 2 " << item << endl;
58 this_thread::sleep_for(chrono::milliseconds(WORK_DELAY_MS));
60 }
61};
62
63int main(int argc, char *argv[])
64{
65 if (argc == 1)
66 {
67 cout << "test-con-queue -- demonstrates QueueTheadsPool concurrent processing\n"
68 << "\n"
69 << "Creates a pool of worker threads backed by a shared queue. Each thread\n"
70 << "runs one of two event types (Event1 / Event2), both of which sleep for\n"
71 << WORK_DELAY_MS << " ms to simulate work. The test enqueues items, lets workers\n"
72 << "run for a while, then suspends, sleeps, resumes, and finally shuts down.\n"
73 << "\n"
74 << "Usage:\n"
75 << " " << argv[0] << " <num_threads> <num_items> <secs>\n"
76 << "\n"
77 << "Arguments:\n"
78 << " num_threads Number of worker threads to create (positive integer)\n"
79 << " num_items Number of integer items to enqueue\n"
80 << " secs Seconds to let workers run in each active phase\n"
81 << "\n"
82 << "Example:\n"
83 << " " << argv[0] << " 4 100 3\n"
84 << " Creates 4 threads, enqueues 100 items, runs for 3 s, suspends,\n"
85 << " sleeps 5 s, resumes for another 3 s, then shuts down.\n";
86 return 0;
87 }
88
89 if (argc < 4)
90 {
91 cerr << "Usage: " << argv[0] << " <num_threads> <num_items> <secs>" << endl;
92 return 1;
93 }
94
95 char * endptr = nullptr;
96 errno = 0;
97 if (argv[1][0] == '-')
98 {
99 cerr << "Invalid num_threads: " << argv[1] << endl;
100 return 1;
101 }
102 const size_t num_threads = strtoul(argv[1], &endptr, 10);
103 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or num_threads == 0)
104 {
105 cerr << "Invalid num_threads: " << argv[1] << endl;
106 return 1;
107 }
108
109 if (argv[2][0] == '-')
110 {
111 cerr << "Invalid num_items: " << argv[2] << endl;
112 return 1;
113 }
114 const size_t num_items = strtoul(argv[2], &endptr, 10);
115 if (errno != 0 or endptr == argv[2] or *endptr != '\0')
116 {
117 cerr << "Invalid num_items: " << argv[2] << endl;
118 return 1;
119 }
120
121 if (argv[3][0] == '-')
122 {
123 cerr << "Invalid secs: " << argv[3] << endl;
124 return 1;
125 }
126 const size_t secs = strtoul(argv[3], &endptr, 10);
127 if (errno != 0 or endptr == argv[3] or *endptr != '\0')
128 {
129 cerr << "Invalid secs: " << argv[3] << endl;
130 return 1;
131 }
132
135 for (size_t i = 0; i < num_threads; ++i)
136 {
137 QueueTheadsPool<int>::Event * ptr = nullptr;
138 if (i % 2 == 0)
139 qpool.create_thread(ptr = new Event1);
140 else
141 qpool.create_thread(ptr = new Event2);
142 event_list.append(ptr);
143 }
144
145 cout << "Inserting " << num_items << " to the queue" << endl;
146 for (size_t i = 0; i < num_items; ++i)
147 {
148 cout << "Inserting " << i << endl;
149 qpool.put(i);
150 }
151 cout << "Done" << endl
152 << endl
153 << "Resuming ... " << endl;
154 qpool.resume();
155
156 cout << endl
157 << "Sleeping for " << secs << " seconds" << endl;
158 this_thread::sleep_for(chrono::seconds(secs));
159 cout << endl
160 << "Done" << endl
161 << endl
162 << "List of event counters" << endl;
163 event_list.for_each([] (auto e)
164 {
165 cout << " Event count = " << e->get_count() << endl;
166 });
167
168 cout << "done" << endl
169 << endl
170 << "Suspending (cooperative: in-flight tasks continue in run())" << endl;
171 qpool.suspend();
172 cout << "Done" << endl
173 << endl
174 << "Sleeping for 5 secs" << endl;
175 this_thread::sleep_for(chrono::seconds(5));
176 cout << "done" << endl
177 << endl
178 << "Resuming while other " << secs << " seconds" << endl
179 << endl;
180 qpool.resume();
181 this_thread::sleep_for(chrono::seconds(secs));
182 cout << endl
183 << "Done" << endl
184 << endl
185 << "List of event counters" << endl;
186 event_list.for_each([] (auto e)
187 {
188 cout << " Event count = " << e->get_count() << endl;
189 });
190
191 cout << "done" << endl
192 << endl
193 << "Shutdown" << endl;
194 qpool.shutdown();
195 cout << "Done" << endl;
196
197 return 0;
198}
int main()
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:796
Pool of consumer threads form a queue.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
STL namespace.
Queue-based consumer thread pool.
void run() override
this is a item extracted from the queue
void run() override
this is a item extracted from the queue
static constexpr size_t WORK_DELAY_MS