Skip to content

Instantly share code, notes, and snippets.

@heejune
Created February 19, 2017 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heejune/47d3df9cb1dc6bd7f4a2869da8febca3 to your computer and use it in GitHub Desktop.
Save heejune/47d3df9cb1dc6bd7f4a2869da8febca3 to your computer and use it in GitHub Desktop.
resumable test #2
// resumable-idea.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
using namespace std::experimental;
struct resumable_thing
{
struct promise_type
{
resumable_thing get_return_object()
{
return resumable_thing(coroutine_handle<promise_type>::from_promise(*this));
}
auto initial_suspend() { return suspend_never{}; }
auto final_suspend() { return suspend_never{}; }
void return_void() {}
};
coroutine_handle<promise_type> _coroutine = nullptr;
resumable_thing() = default;
resumable_thing(resumable_thing const&) = delete;
resumable_thing& operator=(resumable_thing const&) = delete;
resumable_thing(resumable_thing&& other)
: _coroutine(other._coroutine) {
other._coroutine = nullptr;
}
resumable_thing& operator = (resumable_thing&& other) {
if (&other != this) {
_coroutine = other._coroutine;
other._coroutine = nullptr;
}
}
explicit resumable_thing(coroutine_handle<promise_type> coroutine) : _coroutine(coroutine)
{
}
~resumable_thing()
{
if (_coroutine) { _coroutine.destroy(); }
}
void resume() { _coroutine.resume(); }
};
resumable_thing counter() {
cout << "counter: called\n";
for (unsigned i = 1; ; i++)
{
co_await std::experimental::suspend_always{};
cout << "counter:: resumed (#" << i << ")\n";
}
}
void main()
{
cout << "main: calling counter\n";
resumable_thing the_counter = counter();
cout << "main: resuming counter\n";
the_counter.resume();
the_counter.resume();
cout << "main: done\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment