co_wait will setup a state machine to return, future has to call get() to return
#include "stdafx.h"
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>
using namespace std;
using namespace std::experimental;
std::future<int> produceAsync()
{
return std::async(std::launch::async, [] {
cout << " future thread id:"<< std::this_thread::get_id()<<endl;
std::this_thread::sleep_for(5s);
return 42;
});
}
std::future<void> ConsumeAsync()
{
auto r = co_await produceAsync(); // set up resume state machine
cout << r << endl;
}
int main()
{
cout << "main thread id:" << std::this_thread::get_id() <<endl;
ConsumeAsync();
auto r = produceAsync();
//cout << r.get() << endl; //get causes return, no SM setup
std::string s;
getline(cin, s);
return 0;
}
Tuesday, October 25, 2016
co_await vs. future.get()
Monday, October 24, 2016
generator pipeline and accumulate
#include "stdafx.h"
#include <experimental\generator>
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <numeric>
using namespace std;
using namespace std::experimental;
std::experimental::generator<int> gen()
{
for (int i = 0;; ++i)
co_yield i;
};
std::experimental::generator<int> take_until(std::experimental::generator<int> g, int sentinel)
{
for (auto v : g)
{
if (v == sentinel)
break;
co_yield v;
}
}
int main()
{
auto g1 = take_until(gen(), 10);
auto a = std::accumulate(g1.begin(), g1.end(), 0);
cout << a << endl;
std::string s;
getline(cin, s);
return 0;
}
how future made avaitable
This is some information of internal coroutinea
awaiter-co_wait as operator on future and traits into coroutine
struct future_awaiter
{
future<T>& _f;
bool await_ready() {
_f.then([ch]() {});
return _f.is_ready();
}
void await_suspend(coroutine_handle<> ch)
{
_f.then([ch]() {ch.resume(); })
}
auto await_resume() { return _f.get(); }
};
template<typename T>
future_awaiter<T> operator co_await(future<T>& value) // transform future=>awaitable
{
return future_awaiter<T>(value);
};
// make future a coroutine type by specialization
template<typename T, typename... Arguments>
struct coroutine_traits<future<T>, Arguments...>
{
struct promise_type
{
promise<T> _promise;
future<T> get_return_object() { return _promise.get_future(); }
auto initialize_suspend() { return suspend_never{}; }
auto final_suspend() { return suspend_never{}; }
template<typename U>
void return_value(U&& value) { _promise.set_value(std::forward<U>(value)); }
void set_exception(std::exception_ptr ex) { _promise.set_exception(std::move(ex)); }
};
};
co_wait co_return co_yield
coroutine can yield from generator, return from future and wait a async lambda #include "stdafx.h" #include <experimental\generator> #include <experimental\coroutine> #include <future> #include <iostream> #include <string> using namespace std; using namespace std::experimental; future<int> compute() { int ret= co_await std::async([] { return 30; }); co_return ret; } std::experimental::generator<int> gen() { for (int i = 0; i < 10; ++i) co_yield i; }; int main() { for (auto v : gen()) { cout << v << endl; } auto i = compute(); cout << i.get() << endl; std::string s; getline(cin, s); return 0; }
Sunday, October 16, 2016
Delayed Evaluation and Expression template
Instead of loop through vector index multiple times when adding mutiple vector.
could keep ref to sum of two and later loop-through once to reduce temp var
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
// temp var one-by-one
//template<typename T>
//inline vector<T> operator+(const vector<T>& x, const vector<T> y)
//{
// vector<T> sum(x.size());
// for(int i=0;i<x.size();i++)
// sum[i]=x[i]+y[i]; //temp var
// return sum;
//}
// delayed evaluation expression template
template <typename T>
struct vector_sum
{
const vector<T> &x, &y;
vector_sum(const vector<T> &x, const vector<T> &y) :x(x), y(y) {}
T operator[](int i) const { return x[i] + y[i]; } // indexer
};
template<typename T>
vector_sum<T> operator+(const vector<T>& x, const vector<T>& y)
{
return{ x,y }; // delayed evaluation to a single loop
};
template<typename T> class vector // new impl of vector
{
const vector<T> &data;
vector& operator=(const vector_sum<T>& that) //assignment
{
check_size(size(that));
int my_size = size(that);
for (int i = 0; i<my_size; ++i)
data[i] = that[i]; // now loop through.
return *this;
}
};
int main() {
std::vector<int> x = { 1,2,3 };
std::vector<int> y = { 4,5,6 };
auto s = x + y;
cout << s[2] << endl;
return 0;
}
Saturday, October 15, 2016
type traits compile time if in typedef
#include "stdafx.h"
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;
template<bool cond, typename T, typename ElseT>
struct conditional2 {
using type = T;
};
template<typename T, typename ElseT>
struct conditional2<false, T, ElseT>
{
using type = ElseT;
};
int main() {
typedef conditional2<true, int, char>::type T1;
typedef conditional2<1 == 2, int, char>::type T2;
cout << typeid(T1).name() << " " << typeid(T2).name() << endl;
return 0;
}
type traits is_const and variadic template extending std::min
Eclipse CDT tool chain has an issue--"using std::is_heap" not declared inso used VS 2015 instead. #include "stdafx.h" #include <iostream> #include <algorithm> #include <type_traits> using namespace std; template<typename T> struct is_const2 { static const bool value = false; }; template<typename T> struct is_const2<const T> // specialization { static const bool value = true; }; template<typename T> inline T min2(const T& t) { return t; } template<typename T, typename ...P> inline auto min2(const T& t, const P& ...p) { using res_type = std::common_type_t<T, P...>; return std::min(res_type(t), res_type(min2(p...))); }; int main() { cout << "test " << is_const2<const int*>::value << endl; cout << "test " << std::is_const<int* const>::value << endl; cout << "test " << is_const2<const int&>::value << endl; const int i = 123; const double j = 456.1; const char k = 'a'; auto m = min2(i,j,k); return 0; }
Subscribe to:
Posts (Atom)