Logo ROOT   6.12/07
Reference Guide
mt305_TFuture.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// \notebook -js
4 /// Shows how to use the Future class of ROOT as a wrapper of std::future.
5 ///
6 /// \macro_code
7 ///
8 /// \author Danilo Piparo
9 /// \date August 2017
10 
11 #include "ROOT/TFuture.hxx"
12 
13 #include <future>
14 #include <iostream>
15 #include <thread>
16 
17 void mt305_TFuture()
18 {
19  using namespace ROOT::Experimental;
20 
21  // future from a packaged_task
22  std::packaged_task<int()> task([]() { return 7; }); // wrap the function
23  TFuture<int> f1 = task.get_future(); // get a future
24  std::thread(std::move(task)).detach(); // launch on a thread
25 
26  // future from an async()
27  TFuture<int> f2 = std::async(std::launch::async, []() { return 8; });
28 
29  // future from a promise
30  std::promise<int> p;
31  TFuture<int> f3(p.get_future());
32  std::thread([&p] { p.set_value(9); }).detach();
33 
34  std::cout << "Waiting..." << std::flush;
35  f1.wait();
36  f2.wait();
37  f3.wait();
38  std::cout << "Done!\nResults are: " << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
39 }
A TFuture class. It can wrap an std::future.
Definition: TFuture.hxx:35
TF1 * f1
Definition: legend1.C:11