Skip to content

Instantly share code, notes, and snippets.

@piusayowale
Created January 12, 2023 19:38
Show Gist options
  • Save piusayowale/78d76175fad4052fc887e6e2a003c1af to your computer and use it in GitHub Desktop.
Save piusayowale/78d76175fad4052fc887e6e2a003c1af to your computer and use it in GitHub Desktop.
pooling event with boost.asio
// prac.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <boost/asio.hpp>
#include <thread>
#include <chrono>
std::chrono::seconds getsecondsPt() {
auto st = std::chrono::high_resolution_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::seconds>(st);
}
int main() {
boost::asio::io_context context;
auto run = [](boost::asio::io_context& context) {
auto start = getsecondsPt();
while (true) {
auto now = getsecondsPt();
if (now >= start + std::chrono::seconds(1)) {
context.post([] {
std::cout << "elapsed\n";
});
start = getsecondsPt();
}
}
};
std::thread thr(run, std::ref(context));
auto work_guard = boost::asio::make_work_guard(context);
context.run();
thr.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment