]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/spawn/parallel_grep.cpp
5 // Copyright (c) 2003-2022 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>
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
;
26 int main(int argc
, char* argv
[])
32 std::cerr
<< "Usage: parallel_grep <string> <files...>\n";
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.
41 // To prevent the output from being garbled, we use a strand to synchronise
43 strand
<thread_pool::executor_type
> output_strand(pool
.get_executor());
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
)
49 std::string input_file
= argv
[argn
];
51 [=](yield_context yield
)
53 std::ifstream
is(input_file
.c_str());
55 std::size_t line_num
= 0;
56 while (std::getline(is
, line
))
58 // If we find a match, send a message to the output.
59 if (line
.find(search_string
) != std::string::npos
)
61 dispatch(output_strand
,
64 std::cout
<< input_file
<< ':' << line
<< std::endl
;
68 // Every so often we yield control to another coroutine.
69 if (++line_num
% 10 == 0)
75 // Join the thread pool to wait for all the spawned tasks to complete.
78 catch (std::exception
& e
)
80 std::cerr
<< "Exception: " << e
.what() << "\n";