@@ -18,13 +18,9 @@ int main()
1818 const auto input = [](messages& chan, std::size_t thread, std::chrono::milliseconds pause) {
1919 thread_local static std::size_t inc = 0U ;
2020
21- while (true ) {
22- if (chan.closed ()) {
23- return ;
24- }
25-
21+ while (!chan.closed ()) {
2622 ++inc;
27- chan << std::string{std::to_string (inc) + " from: " + std::to_string (thread)};
23+ chan << std::string{" Streaming " + std::to_string (inc) + " from thread " + std::to_string (thread)};
2824
2925 std::this_thread::sleep_for (pause);
3026 }
@@ -35,20 +31,17 @@ int main()
3531 in_futures.push_back (std::async (input, std::ref (channel), i, std::chrono::milliseconds{500 }));
3632 }
3733
38- // Stream incoming data to a destination
39- const auto out = [](messages& chan, std::ostream& stream, const std::string& separator) {
40- std::move (chan.begin (), chan.end (), std::ostream_iterator<std::string>(stream, separator.c_str ()));
41- };
42- const auto out_future = std::async (out, std::ref (channel), std::ref (std::cout), " \n " );
43-
4434 // Close the channel after some time
4535 const auto timeout = [](messages& chan, std::chrono::milliseconds after) {
4636 std::this_thread::sleep_for (after);
4737 chan.close ();
4838 };
4939 const auto timeout_future = std::async (timeout, std::ref (channel), std::chrono::milliseconds{3000U });
5040
51- out_future.wait ();
41+ // Stream incoming data to a destination
42+ std::move (channel.begin (), channel.end (), std::ostream_iterator<std::string>(std::cout, " \n " ));
43+
44+ // Wait for other threads
5245 for (auto & future : in_futures) {
5346 future.wait ();
5447 }
0 commit comments