70int main(
int argc,
char *argv[])
74 cout <<
"test-con-queue -- demonstrates QueueTheadsPool concurrent processing\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"
82 <<
" " << argv[0] <<
" <num_threads> <num_items> <secs>\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"
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";
98 cerr <<
"Usage: " << argv[0] <<
" <num_threads> <num_items> <secs>" << endl;
102 char * endptr =
nullptr;
104 if (argv[1][0] ==
'-')
106 cerr <<
"Invalid num_threads: " << argv[1] << endl;
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)
112 cerr <<
"Invalid num_threads: " << argv[1] << endl;
116 if (argv[2][0] ==
'-')
118 cerr <<
"Invalid num_items: " << argv[2] << endl;
121 const size_t num_items = strtoul(argv[2], &endptr, 10);
122 if (errno != 0 or endptr == argv[2] or *endptr !=
'\0')
124 cerr <<
"Invalid num_items: " << argv[2] << endl;
128 if (argv[3][0] ==
'-')
130 cerr <<
"Invalid secs: " << argv[3] << endl;
133 const size_t secs = strtoul(argv[3], &endptr, 10);
134 if (errno != 0 or endptr == argv[3] or *endptr !=
'\0')
136 cerr <<
"Invalid secs: " << argv[3] << endl;
142 for (
size_t i = 0; i < num_threads; ++i)
152 cout <<
"Inserting " << num_items <<
" to the queue" << endl;
153 for (
size_t i = 0; i < num_items; ++i)
155 cout <<
"Inserting " << i << endl;
158 cout <<
"Done" << endl
160 <<
"Resuming ... " << endl;
164 <<
"Sleeping for " << secs <<
" seconds" << endl;
165 this_thread::sleep_for(chrono::seconds(secs));
169 <<
"List of event counters" << endl;
172 cout <<
" Event count = " << e->get_count() << endl;
175 cout <<
"done" << endl
177 <<
"Suspending (cooperative: in-flight tasks continue in run())" << endl;
179 cout <<
"Done" << endl
181 <<
"Sleeping for 5 secs" << endl;
182 this_thread::sleep_for(chrono::seconds(5));
183 cout <<
"done" << endl
185 <<
"Resuming while other " << secs <<
" seconds" << endl
188 this_thread::sleep_for(chrono::seconds(secs));
192 <<
"List of event counters" << endl;
195 cout <<
" Event count = " << e->get_count() << endl;
198 cout <<
"done" << endl
200 <<
"Shutdown" << endl;
202 cout <<
"Done" << endl;