Linux C++ Boost ASIO 简单的周期性定时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4267546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Boost ASIO simple periodic timer?
提问by Scott
I want a very simple periodic timer to call my code every 50ms. I could make a thread that sleeps for 50ms all the time (but that's a pain)... I could start looking into Linux API's for making timers (but it's not portable)...
我想要一个非常简单的定期计时器每 50 毫秒调用一次我的代码。我可以制作一个一直休眠 50 毫秒的线程(但这很痛苦)……我可以开始研究 Linux API 来制作计时器(但它不是可移植的)……
I'd liketo use boost.. I'm just not sure it's possible. Does boost provide this functionality?
我想使用boost。我只是不知道这是可能的。boost 是否提供此功能?
采纳答案by Default
The second example on Boosts Asio tutorials explains it.
You can find it here.
Boosts Asio 教程中的第二个示例对此进行了解释。
你可以在这里找到它。
After that, check the 3rd exampleto see how you can call it again with a periodic time intervall
之后,检查第三个示例以了解如何以周期性时间间隔再次调用它
回答by Lucio Paiva
A very simple, but fully functional example:
一个非常简单但功能齐全的示例:
#include <iostream>
#include <boost/asio.hpp>
boost::asio::io_service io_service;
boost::posix_time::seconds interval(1); // 1 second
boost::asio::deadline_timer timer(io_service, interval);
void tick(const boost::system::error_code& /*e*/) {
std::cout << "tick" << std::endl;
// Reschedule the timer for 1 second in the future:
timer.expires_at(timer.expires_at() + interval);
// Posts the timer event
timer.async_wait(tick);
}
int main(void) {
// Schedule the timer for the first time:
timer.async_wait(tick);
// Enter IO loop. The timer will fire for the first time 1 second from now:
io_service.run();
return 0;
}
Notice that it is very important to call expires_at()
to set a new expiration time, otherwise the timer will fire immediately because it's current due time already expired.
请注意,调用expires_at()
以设置新的到期时间非常重要,否则计时器将立即触发,因为它的当前到期时间已经到期。
回答by jaxkewl
To further expand on this simple example. It will block the execution as was said in the comments, so if you want more io_services running, you should run them in a thread like so...
进一步扩展这个简单的例子。它会像评论中所说的那样阻塞执行,所以如果你想要更多的 io_services 运行,你应该像这样在一个线程中运行它们......
boost::asio::io_service io_service;
boost::asio::io_service service2;
timer.async_wait(tick);
boost::thread_group threads;
threads.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
service2.run();
threads.join_all();
回答by Simon Pfister
As I had some issues with prior answers, here is my example:
由于我对先前的答案有一些问题,这是我的示例:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
void print(const boost::system::error_code&, boost::asio::deadline_timer* t,int* count)
{
if (*count < 5)
{
std::cout << *count << std::endl;
++(*count);
t->expires_from_now(boost::posix_time::seconds(1));
t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count));
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count));
io.run();
std::cout << "Final count is " << count << std::endl;
return 0;
}
it did what it supposed to do: counting to five. May it help someone.
它做了它应该做的:数到五。愿它帮助某人。