summaryrefslogtreecommitdiff
path: root/doc/html/boost_asio/example/cpp03/spawn/parallel_grep.cpp
blob: 63aa2fc44799a53ade071b0fe412f650ce9333af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// parallel_grep.cpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include <iostream>
#include <string>

using boost::asio::dispatch;
using boost::asio::spawn;
using boost::asio::strand;
using boost::asio::thread_pool;
using boost::asio::yield_context;

void print_match(std::string input_file, std::string line)
{
  std::cout << input_file << ':' << line << std::endl;
}

void search_file(std::string search_string, std::string input_file,
    strand<thread_pool::executor_type> output_strand, yield_context yield)
{
  std::ifstream is(input_file.c_str());
  std::string line;
  std::size_t line_num = 0;
  while (std::getline(is, line))
  {
    // If we find a match, send a message to the output.
    if (line.find(search_string) != std::string::npos)
    {
      dispatch(output_strand, boost::bind(&print_match, input_file, line));
    }

    // Every so often we yield control to another coroutine.
    if (++line_num % 10 == 0)
      post(yield);
  }
}

int main(int argc, char* argv[])
{
  try
  {
    if (argc < 2)
    {
      std::cerr << "Usage: parallel_grep <string> <files...>\n";
      return 1;
    }

    // We use a fixed size pool of threads for reading the input files. The
    // number of threads is automatically determined based on the number of
    // CPUs available in the system.
    thread_pool pool;

    // To prevent the output from being garbled, we use a strand to synchronise
    // printing.
    strand<thread_pool::executor_type> output_strand(pool.get_executor());

    // Spawn a new coroutine for each file specified on the command line.
    std::string search_string = argv[1];
    for (int argn = 2; argn < argc; ++argn)
    {
      std::string input_file = argv[argn];
      spawn(pool, boost::bind(&search_file,
            search_string, input_file, output_strand, _1));
    }

    // Join the thread pool to wait for all the spawned tasks to complete.
    pool.join();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}