#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <thread>
#include <iostream>
using namespace boost::asio;
int main()
{
io_service ioservice;
steady_timer timer1{ ioservice, std::chrono::seconds(1) };
timer1.async_wait([](const boost::system::error_code & /* e */) {
for (int i = 0; i < 5; ++i) {
std::cout << "message from timer1." << std::this_thread::get_id() << std::endl;
}
});
//steady_timer timer2{ ioservice, std::chrono::seconds(1) };
//timer1.async_wait([](const boost::system::error_code & /* e */) {
// for (int i = 0; i < 5; ++i) {
// std::cout << "message from timer2." << std::this_thread::get_id() << std::endl;
// }
//});
std::thread thread1{ [&ioservice]() { ioservice.run(); } };
std::thread thread2{ [&ioservice]() { ioservice.run(); } };
thread1.join();
thread2.join();
return 0;
}
boost asio的异步事件处理函数是在执行异步事件的run函数所在的线程里面执行的
最新推荐文章于 2025-06-07 13:47:10 发布