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