Created
March 31, 2020 23:30
-
-
Save stefanofiorentino/199ada95df08c9c8fcc14bf8fa61cd30 to your computer and use it in GitHub Desktop.
libuvw example use of async and thread [uvw is a libuv c++ wrapper]
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 <uvw.hpp> | |
#include <cassert> | |
#include <iostream> | |
#include <memory> | |
#include <chrono> | |
int main() { | |
using namespace std::chrono_literals; | |
// get the default loop | |
auto loop = uvw::Loop::getDefault(); | |
// to keep loop running | |
auto idle_handle = loop->resource<uvw::IdleHandle>(); | |
idle_handle->start(); | |
// here an async handle is created | |
auto async_handle = loop->resource<uvw::AsyncHandle>(); | |
async_handle->on<uvw::CloseEvent>([loop](auto const&, auto&){ | |
loop->stop(); | |
}); | |
auto thread_handle = loop->resource<uvw::Thread>([](std::shared_ptr<void> data){ | |
auto async_handle = std::static_pointer_cast<uvw::AsyncHandle>(data); | |
uv_sleep(1000); | |
async_handle->close(); | |
}, async_handle); | |
thread_handle->run(); | |
loop->run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment