Skip to content

Instantly share code, notes, and snippets.

@bionicbeagle
Created March 16, 2013 21:45
Show Gist options
  • Save bionicbeagle/5178463 to your computer and use it in GitHub Desktop.
Save bionicbeagle/5178463 to your computer and use it in GitHub Desktop.
Example of using libuv with stateless lambdas
// Initialize uv_async_t for shutting down event loop
m_shutdownAsync.data = this;
uv_async_init(m_eventLoop, &m_shutdownAsync, [](uv_async_t* handle, int status) {
auto thisPtr = reinterpret_cast<DiscoService*>(handle->data);
uv_close((uv_handle_t*) handle, nullptr);
uv_close((uv_handle_t*) &thisPtr->m_recvSocket, nullptr);
uv_close((uv_handle_t*) &thisPtr->m_sendSocket, nullptr);
uv_close((uv_handle_t*) &thisPtr->m_timer, nullptr);
uv_close((uv_handle_t*) &thisPtr->m_heartbeat, nullptr);
});
// Start a repeating timer
m_timer.data = this;
uv_timer_init(m_eventLoop, &m_timer);
uv_timer_start(&m_timer, [](uv_timer_t* handle, int status) {
auto thisPtr = reinterpret_cast<DiscoService*>(handle->data);
printf("tick\n");
}, 1000, 30000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment