| 340 | }; |
| 341 | |
| 342 | int main(int argc, char* argv[]) |
| 343 | { |
| 344 | // Check command line arguments. |
| 345 | if (argc != 2) |
| 346 | { |
| 347 | std::cerr << |
| 348 | "Usage: http-crawl <threads>\n" << |
| 349 | "Example:\n" << |
| 350 | " http-crawl 100\n"; |
| 351 | return EXIT_FAILURE; |
| 352 | } |
| 353 | auto const threads = std::max<int>(1, std::atoi(argv[1])); |
| 354 | |
| 355 | // The io_context is used to aggregate the statistics |
| 356 | net::io_context ioc; |
| 357 | |
| 358 | // The report holds the aggregated statistics |
| 359 | crawl_report report{ioc}; |
| 360 | |
| 361 | timer t; |
| 362 | |
| 363 | // Create and launch the worker threads. |
| 364 | std::vector<std::thread> workers; |
| 365 | workers.reserve(threads + 1); |
| 366 | for(int i = 0; i < threads; ++i) |
| 367 | { |
| 368 | // Each worker will eventually add some data to the aggregated |
| 369 | // report. Outstanding work is tracked in each worker to |
| 370 | // represent the forthcoming delivery of this data by that |
| 371 | // worker. |
| 372 | auto reporting_work = net::require( |
| 373 | ioc.get_executor(), |
| 374 | net::execution::outstanding_work.tracked); |
| 375 | |
| 376 | workers.emplace_back( |
| 377 | [&report, reporting_work] { |
| 378 | // We use a separate io_context for each worker because |
| 379 | // the asio resolver simulates asynchronous operation using |
| 380 | // a dedicated worker thread per io_context, and we want to |
| 381 | // do a lot of name resolutions in parallel. |
| 382 | net::io_context ioc; |
| 383 | std::make_shared<worker>(report, ioc)->run(); |
| 384 | ioc.run(); |
| 385 | }); |
| 386 | } |
| 387 | |
| 388 | // Add another thread to run the main io_context which |
| 389 | // is used to aggregate the statistics |
| 390 | workers.emplace_back( |
| 391 | [&ioc] |
| 392 | { |
| 393 | ioc.run(); |
| 394 | }); |
| 395 | |
| 396 | // Now block until all threads exit |
| 397 | for(std::size_t i = 0; i < workers.size(); ++i) |
| 398 | workers[i].join(); |
| 399 |
nothing calls this directly
no test coverage detected