]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/spawn/parallel_grep.cpp
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
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.hpp>
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
;
28 void print_match(std::string input_file
, std::string line
)
30 std::cout
<< input_file
<< ':' << line
<< std::endl
;
33 void search_file(std::string search_string
, std::string input_file
,
34 strand
<thread_pool::executor_type
> output_strand
, yield_context yield
)
36 std::ifstream
is(input_file
.c_str());
38 std::size_t line_num
= 0;
39 while (std::getline(is
, line
))
41 // If we find a match, send a message to the output.
42 if (line
.find(search_string
) != std::string::npos
)
44 dispatch(output_strand
, boost::bind(&print_match
, input_file
, line
));
47 // Every so often we yield control to another coroutine.
48 if (++line_num
% 10 == 0)
53 int main(int argc
, char* argv
[])
59 std::cerr
<< "Usage: parallel_grep <string> <files...>\n";
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.
68 // To prevent the output from being garbled, we use a strand to synchronise
70 strand
<thread_pool::executor_type
> output_strand(pool
.get_executor());
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
)
76 std::string input_file
= argv
[argn
];
77 spawn(pool
, boost::bind(&search_file
,
78 search_string
, input_file
, output_strand
, _1
));
81 // Join the thread pool to wait for all the spawned tasks to complete.
84 catch (std::exception
& e
)
86 std::cerr
<< "Exception: " << e
.what() << "\n";