| 
									
										
										
										
											2017-10-12 16:28:19 +05:30
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <future>
 | 
					
						
							|  |  |  | #include <thread>
 | 
					
						
							|  |  |  | #include "unity.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:28:19 +05:30
										 |  |  | #if __GTHREADS && __GTHREADS_CXX0X
 | 
					
						
							|  |  |  | TEST_CASE("C++ future", "[std::future]") | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // future from a packaged_task
 | 
					
						
							|  |  |  |     std::packaged_task<int()> task([]{ return 7; }); // wrap the function
 | 
					
						
							|  |  |  |     std::future<int> f1 = task.get_future();  // get a future
 | 
					
						
							|  |  |  |     std::thread t(std::move(task)); // launch on a thread
 | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:28:19 +05:30
										 |  |  |     // future from an async()
 | 
					
						
							|  |  |  |     std::future<int> f2 = std::async(std::launch::async, []{ return 8; }); | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:28:19 +05:30
										 |  |  |     // future from a promise
 | 
					
						
							|  |  |  |     std::promise<int> p; | 
					
						
							|  |  |  |     std::future<int> f3 = p.get_future(); | 
					
						
							|  |  |  |     std::thread( [&p]{ p.set_value_at_thread_exit(9); }).detach(); | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:28:19 +05:30
										 |  |  |     std::cout << "Waiting..." << std::flush; | 
					
						
							|  |  |  |     f1.wait(); | 
					
						
							|  |  |  |     f2.wait(); | 
					
						
							|  |  |  |     f3.wait(); | 
					
						
							|  |  |  |     std::cout << "Done!\nResults are: " | 
					
						
							|  |  |  |               << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n'; | 
					
						
							|  |  |  |     t.join(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |