parallel_grep.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // parallel_grep.cpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio/dispatch.hpp>
  11. #include <boost/asio/post.hpp>
  12. #include <boost/asio/spawn.hpp>
  13. #include <boost/asio/strand.hpp>
  14. #include <boost/asio/thread_pool.hpp>
  15. #include <fstream>
  16. #include <iostream>
  17. #include <string>
  18. using boost::asio::dispatch;
  19. using boost::asio::spawn;
  20. using boost::asio::strand;
  21. using boost::asio::thread_pool;
  22. using boost::asio::yield_context;
  23. int main(int argc, char* argv[])
  24. {
  25. try
  26. {
  27. if (argc < 2)
  28. {
  29. std::cerr << "Usage: parallel_grep <string> <files...>\n";
  30. return 1;
  31. }
  32. // We use a fixed size pool of threads for reading the input files. The
  33. // number of threads is automatically determined based on the number of
  34. // CPUs available in the system.
  35. thread_pool pool;
  36. // To prevent the output from being garbled, we use a strand to synchronise
  37. // printing.
  38. strand<thread_pool::executor_type> output_strand(pool.get_executor());
  39. // Spawn a new coroutine for each file specified on the command line.
  40. std::string search_string = argv[1];
  41. for (int argn = 2; argn < argc; ++argn)
  42. {
  43. std::string input_file = argv[argn];
  44. spawn(pool,
  45. [=](yield_context yield)
  46. {
  47. std::ifstream is(input_file.c_str());
  48. std::string line;
  49. std::size_t line_num = 0;
  50. while (std::getline(is, line))
  51. {
  52. // If we find a match, send a message to the output.
  53. if (line.find(search_string) != std::string::npos)
  54. {
  55. dispatch(output_strand,
  56. [=]
  57. {
  58. std::cout << input_file << ':' << line << std::endl;
  59. });
  60. }
  61. // Every so often we yield control to another coroutine.
  62. if (++line_num % 10 == 0)
  63. post(yield);
  64. }
  65. });
  66. }
  67. // Join the thread pool to wait for all the spawned tasks to complete.
  68. pool.join();
  69. }
  70. catch (std::exception& e)
  71. {
  72. std::cerr << "Exception: " << e.what() << "\n";
  73. }
  74. return 0;
  75. }