Aleph-w 7.10.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// QueueTheadsPool is deprecated: it goes in 8.0.0, and ThreadPool
38// (thread_pool.H) replaces it. This example shows it until then.
39# if defined(_MSC_VER) && !defined(__clang__)
40# pragma warning(disable : 4996)
41# else
42# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
43# endif
44# include <q-consumer-threads.H>
45
46using namespace std;
47
48static constexpr size_t WORK_DELAY_MS = 10;
49
50struct Event1 : QueueTheadsPool<int>::Event
51{
52 void run() override
53 {
54 // cout << "Event 1 " << item << endl;
55 this_thread::sleep_for(chrono::milliseconds(WORK_DELAY_MS));
57 }
58};
59
60struct Event2 : QueueTheadsPool<int>::Event
61{
62 void run() override
63 {
64 // cout << "Event 2 " << item << endl;
65 this_thread::sleep_for(chrono::milliseconds(WORK_DELAY_MS));
67 }
68};
69
70int main(int argc, char *argv[])
71{
72 if (argc == 1)
73 {
74 cout << "test-con-queue -- demonstrates QueueTheadsPool concurrent processing\n"
75 << "\n"
76 << "Creates a pool of worker threads backed by a shared queue. Each thread\n"
77 << "runs one of two event types (Event1 / Event2), both of which sleep for\n"
78 << WORK_DELAY_MS << " ms to simulate work. The test enqueues items, lets workers\n"
79 << "run for a while, then suspends, sleeps, resumes, and finally shuts down.\n"
80 << "\n"
81 << "Usage:\n"
82 << " " << argv[0] << " <num_threads> <num_items> <secs>\n"
83 << "\n"
84 << "Arguments:\n"
85 << " num_threads Number of worker threads to create (positive integer)\n"
86 << " num_items Number of integer items to enqueue\n"
87 << " secs Seconds to let workers run in each active phase\n"
88 << "\n"
89 << "Example:\n"
90 << " " << argv[0] << " 4 100 3\n"
91 << " Creates 4 threads, enqueues 100 items, runs for 3 s, suspends,\n"
92 << " sleeps 5 s, resumes for another 3 s, then shuts down.\n";
93 return 0;
94 }
95
96 if (argc < 4)
97 {
98 cerr << "Usage: " << argv[0] << " <num_threads> <num_items> <secs>" << endl;
99 return 1;
100 }
101
102 char * endptr = nullptr;
103 errno = 0;
104 if (argv[1][0] == '-')
105 {
106 cerr << "Invalid num_threads: " << argv[1] << endl;
107 return 1;
108 }
109 const size_t num_threads = strtoul(argv[1], &endptr, 10);
110 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or num_threads == 0)
111 {
112 cerr << "Invalid num_threads: " << argv[1] << endl;
113 return 1;
114 }
115
116 if (argv[2][0] == '-')
117 {
118 cerr << "Invalid num_items: " << argv[2] << endl;
119 return 1;
120 }
121 const size_t num_items = strtoul(argv[2], &endptr, 10);
122 if (errno != 0 or endptr == argv[2] or *endptr != '\0')
123 {
124 cerr << "Invalid num_items: " << argv[2] << endl;
125 return 1;
126 }
127
128 if (argv[3][0] == '-')
129 {
130 cerr << "Invalid secs: " << argv[3] << endl;
131 return 1;
132 }
133 const size_t secs = strtoul(argv[3], &endptr, 10);
134 if (errno != 0 or endptr == argv[3] or *endptr != '\0')
135 {
136 cerr << "Invalid secs: " << argv[3] << endl;
137 return 1;
138 }
139
142 for (size_t i = 0; i < num_threads; ++i)
143 {
144 QueueTheadsPool<int>::Event * ptr = nullptr;
145 if (i % 2 == 0)
146 qpool.create_thread(ptr = new Event1);
147 else
148 qpool.create_thread(ptr = new Event2);
149 event_list.append(ptr);
150 }
151
152 cout << "Inserting " << num_items << " to the queue" << endl;
153 for (size_t i = 0; i < num_items; ++i)
154 {
155 cout << "Inserting " << i << endl;
156 qpool.put(i);
157 }
158 cout << "Done" << endl
159 << endl
160 << "Resuming ... " << endl;
161 qpool.resume();
162
163 cout << endl
164 << "Sleeping for " << secs << " seconds" << endl;
165 this_thread::sleep_for(chrono::seconds(secs));
166 cout << endl
167 << "Done" << endl
168 << endl
169 << "List of event counters" << endl;
170 event_list.for_each([] (auto e)
171 {
172 cout << " Event count = " << e->get_count() << endl;
173 });
174
175 cout << "done" << endl
176 << endl
177 << "Suspending (cooperative: in-flight tasks continue in run())" << endl;
178 qpool.suspend();
179 cout << "Done" << endl
180 << endl
181 << "Sleeping for 5 secs" << endl;
182 this_thread::sleep_for(chrono::seconds(5));
183 cout << "done" << endl
184 << endl
185 << "Resuming while other " << secs << " seconds" << endl
186 << endl;
187 qpool.resume();
188 this_thread::sleep_for(chrono::seconds(secs));
189 cout << endl
190 << "Done" << endl
191 << endl
192 << "List of event counters" << endl;
193 event_list.for_each([] (auto e)
194 {
195 cout << " Event count = " << e->get_count() << endl;
196 });
197
198 cout << "done" << endl
199 << endl
200 << "Shutdown" << endl;
201 qpool.shutdown();
202 cout << "Done" << endl;
203
204 return 0;
205}
int main()
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Pool of consumer threads form a queue.
void shutdown()
Make the threads finish, also those of a suspended pool, and wait for them; again,...
void create_thread(ConsumerQueueEvent *event)
Create a thread that consumes the items of the queue with event.
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