Skip to content

Instantly share code, notes, and snippets.

@stefanofiorentino
Created March 31, 2020 23:30
Show Gist options
  • Save stefanofiorentino/199ada95df08c9c8fcc14bf8fa61cd30 to your computer and use it in GitHub Desktop.
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]
#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