-
Notifications
You must be signed in to change notification settings - Fork 3
/
async_futures_promises.cpp
36 lines (30 loc) · 1.01 KB
/
async_futures_promises.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//*****************************************************************************
//
// Author: Michael Price
// License: Attribution-NonCommercial-NoDerivs 3.0 Unported
// http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode
//
//*****************************************************************************
#include <array>
#include <future>
#include <iterator>
#include <iostream>
void even_or_odd ()
{
std::array<int, 10> jumble = {9,6,3,1,7,3,8,0,4,2};
auto is_even = [](int e) { return e%2 == 0; };
auto is_odd = [](int e) { return e%2 != 0; };
auto count_of_evens = std::async([=]()
{
return std::count_if(std::begin(jumble),
std::end(jumble), is_even);
});
auto count_of_odds = std::async([=]()
{
return std::count_if(std::begin(jumble),
std::end(jumble), is_odd);
});
std::cout << "Evens: " << count_of_evens.get()
<< " Odds: " << count_of_odds.get()
<< std::endl;
}