Created
January 17, 2017 21:42
-
-
Save sean-parent/e65372bd7b447db840f437de71c2be2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <iostream> | |
#include <cmath> | |
#include <stlab/future.hpp> | |
#include <stlab/channel.hpp> | |
using namespace stlab; | |
using namespace std; | |
using namespace std::chrono; | |
struct render { | |
process_state_scheduled _state = await_forever; | |
int _params; | |
void await(int params) { | |
_state = await_immediate; | |
_params = params; | |
} | |
double yield() { | |
this_thread::sleep_for(2s); | |
_state = await_forever; | |
return sqrt(_params); | |
} | |
void close() { if (_state == await_immediate) _state = yield_immediate; } | |
const auto& state() const { | |
return _state; | |
} | |
}; | |
int main() { | |
sender<int> send; | |
receiver<int> receive; | |
tie(send, receive) = channel<int>(default_scheduler()); | |
send(1); | |
send(2); | |
send(3); | |
auto hold = receive | |
| render() | |
| [](double x){ cout << x << '\n'; }; | |
receive.set_ready(); | |
#if 0 | |
send(1); | |
send(2); | |
send(3); | |
#endif | |
this_thread::sleep_for(1s); | |
send(4); | |
send(5); | |
this_thread::sleep_for(1s); | |
send(6); | |
send.close(); | |
sleep(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment