play with std::future
This commit is contained in:
parent
415a618522
commit
646b6e44b3
3 changed files with 51 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -18,3 +18,4 @@ classes
|
||||||
lvalues
|
lvalues
|
||||||
bad_alloc
|
bad_alloc
|
||||||
rtti
|
rtti
|
||||||
|
future
|
||||||
|
|
|
@ -18,3 +18,6 @@ add_executable(classes classes.cpp)
|
||||||
add_executable(lvalues lvalues.cpp)
|
add_executable(lvalues lvalues.cpp)
|
||||||
add_executable(bad_alloc bad_alloc.cpp)
|
add_executable(bad_alloc bad_alloc.cpp)
|
||||||
add_executable(rtti rtti.cpp)
|
add_executable(rtti rtti.cpp)
|
||||||
|
|
||||||
|
add_executable(future future.cpp)
|
||||||
|
set_target_properties(future PROPERTIES LINK_FLAGS "-pthread")
|
||||||
|
|
47
future.cpp
Normal file
47
future.cpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// future example
|
||||||
|
// adapted from http://www.cplusplus.com/reference/future/future/
|
||||||
|
|
||||||
|
#include <iostream> // std::cout
|
||||||
|
#include <future> // std::async, std::future
|
||||||
|
#include <chrono> // std::chrono::milliseconds
|
||||||
|
|
||||||
|
// a non-optimized way of checking for prime numbers.
|
||||||
|
bool is_prime (int x) {
|
||||||
|
for (int i=2; i<x; ++i) {
|
||||||
|
if (x%i == 0) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void explicitly_waiting() {
|
||||||
|
int p = 817504243;
|
||||||
|
|
||||||
|
// call function asynchronously:
|
||||||
|
std::future<bool> fut = std::async(std::launch::async, is_prime, p);
|
||||||
|
|
||||||
|
// do something while waiting for function to set future:
|
||||||
|
std::cout << "checking, please wait" << std::flush;
|
||||||
|
std::chrono::milliseconds span(500);
|
||||||
|
while (fut.wait_for(span) == std::future_status::timeout) {
|
||||||
|
std::cout << '.' << std::flush;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
bool x = fut.get(); // retrieve return value
|
||||||
|
std::cout << p << " " << (x?"is":"is not") << " prime." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void implicitly_waiting() {
|
||||||
|
int p2 = 838041641;
|
||||||
|
|
||||||
|
std::future<bool> fut = std::async(std::launch::async, is_prime, p2);
|
||||||
|
std::cout << "just getting the result, doing an implicit wait():" << std::endl;
|
||||||
|
bool x = fut.get();
|
||||||
|
std::cout << p2 << " " << (x?"is":"is not") << " prime." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
explicitly_waiting();
|
||||||
|
implicitly_waiting();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue