C++11 Multithreading - Futures

26
C++11Multithreading Futures

description

This presentation is about multitasking with std::future. Presentation by Dmytro Gurin (Lead Software Engineer, GlobalLogic, Kyiv), delivered at GlobalLogic C++ TechTalk in Lviv, September 18, 2014. More details - http://www.globallogic.com.ua/press-releases/lviv-cpp-techtalk-coverage

Transcript of C++11 Multithreading - Futures

  • 1. Futures

2. IntroductionMotivationAsynchronous Return Object: futureAsynchronous Provider Objects: async packaged_task promiseContinuation passing styleQ&A 3. [C++03, 1.9.6] The observable behavior of the abstract machine isits sequence of reads and writes to volatile data andcalls to library I/O functions. [C++11, 1.10.1]: A thread of execution (also known as a thread) is asingle flow of control within a program... Theexecution of the entire program consists of anexecution of all of its threads... 4. [C++11, 30.1.1]: The following subclauses describe componentsto create and manage threads (1.10), performmutual exclusion, and communicateconditions and values between threadsSubclause HeaderThreads Mutual exclusion Condition variables Futures 5. Having independent time consuming tasks:// ...device target_device;target_device.initialize(); // get_future();}// ...}; 19. provides the highest level of control over the sharedstate; does not require a function or callable object for populateshared state; requires executing thread explicitly set value/exception toshared state. 20. Yet another implementation for async_exec_service:class async_exec_service {queue> _exec_queue;// worker threads will take functions from _exec_queue// ...void enqueue(function task) {/* put task to _exec_queue */}public:template auto exec(_Function&& f) -> future{typedef decltype(f()) _Result;shared_ptr> result =make_shared>();function task_wrapper = [result, f]{try {result->set_value(f());} catch (...) {result->set_exception(current_exception());}}enqueue(task_wrapper);return result->get_future();}}; 21. . || 22. Currently is not a part of C++ StandardGoing to be included in C++17 (N3857)Already available in boost 23. compose two futures by declaring one to be thecontinuation of another:device target_device;configuration_storage db;future configure = async([&db]{configuration config;db.load(config);return config;}).then([&target_device](future config){// JUST FOR EXAMPLE:// config is ready at this point,// but it doesnt mean we could already configure the device});target_device.initialize(); 24. wait a number of futures for at least one to beready:device target_device;configuration_storage db;configuration config;future tasks[] = {async([&target_device]{ return target_device.initialize(); }),async([&db, &config]{ db.load(config); return true; }),};future>> anyone = when_any(begin(tasks), end(tasks));future anyone_completed = anyone.then([](future>> lst) {// JUST FOR EXAMPLEfor (future& f: lst) {if (f.is_ready())return f.get(); // won't block here}}); 25. wait for a number of futures to be ready:device target_device;configuration_storage db;future init_device = async([&target_device]{return target_device.initialize();});future load_config = async([&db]{configuration config;db.load(config);return config;});future, future>> init_all =when_all(init_device, load_config);future device_ready = init_all.then([&target_device](tuple, future> params){bool hw_ok = get().get();if (!hw_ok) return false;configuration config = get().get();target_device.configure(config);return true;}); 26. Thank You!