Skip to content

Instantly share code, notes, and snippets.

@jmorrill
Created November 7, 2014 00:39
Show Gist options
  • Save jmorrill/4d21bdfbea6b7ad4d6b2 to your computer and use it in GitHub Desktop.
Save jmorrill/4d21bdfbea6b7ad4d6b2 to your computer and use it in GitHub Desktop.
C++ libuv library
/* OUTPUT
tick tock
I'm the second handler of the tick event
I'm on the dispatcher thread
I'm on a thread pool thread
I just got value 42 sent to me and I'm on the dispatcher loop thread
I'm on a thread pool thread and shutting down dispatcher safely
exiting...
*/
using namespace std;
using namespace uvxx;
using namespace uvxx::pplx;
class application : public event_dispatcher_object,
public std::enable_shared_from_this<application>
{
event_dispatcher_timer _timer;
void on_tick(event_dispatcher_timer* sender)
{
cout << "tick tock" << endl;
this->dispatcher()->begin_invoke([]
{
cout << "I'm on the dispatcher thread" << endl;
}).then([]()
{
cout << "I'm on a thread pool thread" << endl;
return 42.0;
}).then([](double value)
{
cout << "I just got value " << value << " sent to me and I'm on the dispatcher loop thread" << endl;
}, task_continuation_context::use_current()).then([this]()
{
cout << "I'm on a thread pool thread and shutting down dispatcher safely" << endl;
dispatcher()->begin_shutdown();
});
}
public:
application()
{
_timer.repeat_set(chrono::seconds(1));
_timer.timeout_set(chrono::seconds(1));
/* Hook the tick event */
_timer.tick_event() += std::bind(&application::on_tick, this, placeholders::_1);
/* Hook tick event with lambda */
_timer.tick_event() += [](event_dispatcher_timer* sender)
{
cout << "I'm the second handler of the tick event" << endl;
};
}
void start()
{
_timer.start();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
auto app = make_shared<application>();
app->start();
/* Does not exit unless shutdown */
event_dispatcher::run();
cout << "exiting..." << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment