mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
see #2: revision of parallelism and concurrency
This commit is contained in:
31
code/7/7.4.futures.cpp
Normal file
31
code/7/7.4.futures.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// 7.2.critical.section.a.cpp
|
||||
// chapter 7 parallelism and concurrency
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
// https://github.com/changkun/modern-cpp-tutorial
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
|
||||
void foo() {
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
// pack a lambda expression that returns 7 into a std::packaged_task
|
||||
std::packaged_task<int()> task([](){return 7;});
|
||||
// get the future of task
|
||||
std::future<int> result = task.get_future(); // run task in a thread
|
||||
std::thread(std::move(task)).detach();
|
||||
std::cout << "waiting...";
|
||||
result.wait(); // block until future has arrived
|
||||
// output result
|
||||
std::cout << "done!" << std:: endl << "future result is " << result.get() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user