我想用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