आप सी ++ आपको लगता है कि के लिए Boost ASIO टाइमर का उपयोग कर सकते में रचना कर रहे हैं के बाद से। मैं भी एक टाइमर उन पर आधारित वर्ग तैयार किया गया है और यह अच्छी तरह से काम करता है और किसी भी धागे के बिना - यह ओएस के लिए अतुल्यकालिक कॉल का उपयोग करता है, तो मूल रूप से आप सिर्फ एक कॉलबैक जो जब घड़ी समाप्त हो रहा है बुलाया जाएगा परिभाषित करने के लिए और फिर घड़ी के कॉल async_wait फ़ंक्शन, जो गैर-अवरुद्ध है। जब आप अपना टाइमर ऑब्जेक्ट घोषित करते हैं तो आपको इसे io_service ऑब्जेक्ट पास करना होगा जो ओएसआई के लिए एएसआईओ इंटरफ़ेस है। यह ऑब्जेक्ट आपके एसिंक अनुरोधों और कॉलबैक की सेवा के लिए ज़िम्मेदार है, इसलिए इसके लिए आप अपनी अवरुद्ध विधि रन पर कॉल कर सकते हैं। मेरे मामले में मेरे पास मुख्य धागा अवरुद्ध नहीं हो सका, इसलिए मेरे पास सिर्फ एक धागा था जिसमें यह अनूठा कॉल अवरुद्ध हो रहा था।
यहाँ
आप कैसे बूस्ट ASIO async टाइमर का उपयोग करने के बारे में उदाहरण मिल सकते हैं:
http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/tutorial/tuttimer2.html
मेरे AbstractAsioTimer वर्ग subclassed है ताकि onTimerTick विधि व्युत्पन्न के लिए विशिष्ट होगा डिजाइन किया गया था वर्ग समाप्त होता है। अपनी आवश्यकताओं के लिए थोड़ा अलग हो सकता है है, यह एक अच्छा प्रारंभिक बिंदु हो सकता है:
abstractasiotimer.hpp:
#ifndef _ABSTRACTASIOTIMER_HPP_
#define _ABSTRACTASIOTIMER_HPP_
#include <boost/asio.hpp>
/**
* Encapsulates a POSIX timer with microsecond resolution
*/
class AbstractAsioTimer
{
public:
/**
* Instantiates timer with the desired period
* @param io ASIO interface object to the SO
* @param timeout time in microseconds for the timer handler to be executed
*/
AbstractAsioTimer(boost::asio::io_service& io, unsigned int timeout);
/**
* Destructor
*/
virtual ~AbstractAsioTimer();
/**
* Starts timer operation
*/
void timerStart();
/**
* Stops timer operation
*/
void timerStop();
/**
* Returns timer operation state
*/
bool isRunning() const;
/**
* Returns a reference to the underlying io_service
*/
boost::asio::io_service& get_io_service();
protected:
/**
* Timer handler to execute user specific code
* @note must be reimplemented in derived classes
*/
virtual void onTimerTick() = 0;
private:
/**
* Callback to be executed on timer expiration. It is responsible
* for calling the 'onTimerTick' method and restart the timer if
* it remains active
*/
void timerExpired(const boost::system::error_code& error);
boost::asio::deadline_timer timer; /**< ASIO timer object */
unsigned int timeout; /**< Timer period in microseconds */
bool running; /**< Flag to indicate whether the timer is active */
};
#endif
abstractasiotimer.cpp:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/concept_check.hpp>
#include "abstractasiotimer.hpp"
using namespace boost::asio;
AbstractAsioTimer::AbstractAsioTimer(boost::asio::io_service& io,
unsigned int timeout):
timer(io), timeout(timeout),
running(false)
{
}
AbstractAsioTimer::~AbstractAsioTimer()
{
running = false;
timer.cancel();
}
void AbstractAsioTimer::timerExpired(const boost::system::error_code& error) {
if (!error) {
onTimerTick();
//Restart timer
timerStart();
}
else {
running = false;
std::cerr << "Timer stopped: " << error.message() << std::endl;
}
}
void AbstractAsioTimer::timerStart()
{
timer.expires_from_now(boost::posix_time::microseconds(timeout));
timer.async_wait(boost::bind(&AbstractAsioTimer::timerExpired,
this, placeholders::error));
running = true;
}
void AbstractAsioTimer::timerStop() {
running = false;
timer.cancel();
}
bool AbstractAsioTimer::isRunning() const {
return running;
}
io_service& AbstractAsioTimer::get_io_service()
{
return timer.get_io_service();
}
अधिकांश (या कम से कम से कम कई) टाइमर कार्यान्वयन वास्तव में अवरुद्ध कर रहे हैं। उसमें स्वाभाविक रूप से गलत कुछ भी नहीं है। यह स्पष्ट रूप से इस उद्देश्य पर निर्भर करता है। –