C++螺纹秒表
我正在尝试用 C++ 创建一个秒表,类似于 Java 的 定时器任务。我喜欢他们的库,因为它已经内置了线程。我已经看到 Boost Chrono,但是,它仍在开发中,并且不想使用它。
我当前的实现并不走运(我现在正在记忆中,所以这可能是一些伪代码)。
boost::asio::io_service io;
boost::asio::deadline_timer timer(io);
Initialize()
{
boost::shared_ptr<boost::thread> thread = boost::shared_ptr<boost::thread>(
new boost::thread(
boost::bind(&boost::asio::io_service::run, &io)
);
}
Start()
{
timer.async_wait(
bind(&HandleTimerEvent, this, asio::placeholders::error)
);
}
Stop()
{
timer.cancel
}
Tick()
{
cout << "THE TIME IS: " << timer.current_time << endl; // Pseudo code for current_time.
}
HandleTimerEvent(boost::system::error_code& e)
{
cout << "Timer has ended: " << e << endl;
}
我想要的是让线程连续调用 Tick() 并打印出当前时间。另外,我当前的实现似乎有线程阻塞应用程序的其余部分,这是我绝对不想要的。我该如何做这两件事?
我在线程方面是个菜鸟,所以如果我说了什么似乎不正确的事情,请原谅我。
I am trying to create a stopwatch in C++, something similar to Java's TimerTask. I like their library since it already has threading built in. I have seen Boost Chrono, however, it's still in development and would prefer not to use it.
My current implementation has been without luck (I am going by memory right now so this might be a little pseudo code like).
boost::asio::io_service io;
boost::asio::deadline_timer timer(io);
Initialize()
{
boost::shared_ptr<boost::thread> thread = boost::shared_ptr<boost::thread>(
new boost::thread(
boost::bind(&boost::asio::io_service::run, &io)
);
}
Start()
{
timer.async_wait(
bind(&HandleTimerEvent, this, asio::placeholders::error)
);
}
Stop()
{
timer.cancel
}
Tick()
{
cout << "THE TIME IS: " << timer.current_time << endl; // Pseudo code for current_time.
}
HandleTimerEvent(boost::system::error_code& e)
{
cout << "Timer has ended: " << e << endl;
}
What I would like to have is to have the thread continuously call Tick()
and print out the current time. Also, my current implementation seems to have the thread as blocking the rest of the application which is something I definitely don't want. How would I do both of these things?
I am a noob when it comes to threading so please forgive me if I stated something that doesn't seem right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您几乎已经完成了,因为您正在创建一个新线程来处理 io_service::run() ,您的主线程将不会阻塞。您需要做两件事,
Initialize()
之前调用Start()
(使用相同的 io_service 实例),这样 io_service 就可以做一些事情,否则就会退出!HandleTimer()
方法中,再次调用async_wait
来排队下一个时钟周期,否则 io_service 将退出,因为它无事可做。You nearly have it, because you're creating a new thread to handle
io_service::run()
, your main thread will not block. Two things you need to do,Start()
before youInitialize()
(using the same io_service instance), this way there is something for the io_service to do, else it will quit!HandleTimer()
method, callasync_wait
again to queue up the next tick, else io_service will quit as it has nothing to do..您读过 Boost.Asio 异步 截止日期计时器教程吗?添加多个线程 通过创建线程池来调用
io_service::run
。Have you read the Boost.Asio asynchronous deadline timer tutorial? It's fairly trivial to add multiple threads by creating a thread pool to invoke
io_service::run
.这对我有用:
This works for me: