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