我想用C++创建一个秒表,类似于Java的TimerTask。我喜欢他们的库,因为它已经内置了线程。我已经看到Boost Chrono,但是,它仍在开发中,并且不希望使用它。C++ Threaded秒表
我目前的实现一直没有运气(我现在正在使用内存,所以这可能是一个小的伪代码)。
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()
并打印出当前时间。此外,我目前的实现似乎有阻塞其他应用程序的线程,这是我绝对不想要的。我将如何做这两件事?
当谈到线程时,我是一个小菜,所以请原谅我,如果我说了一些看起来不正确的东西。
你的第二点是那些打耳光的时刻之一。但现在它效果很好。谢谢。 – elmt 2010-11-06 00:36:20
:)很酷,事件的整体安排很难处理,花了我一段时间... – Nim 2010-11-06 00:59:05