I would like a review on this code, and how I could make it better.
#include <boost/bind/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread.hpp>
#include <boost/signals2/mutex.hpp>
#include <stdint.h>
#include <iostream>
#include <vector>
class CallBack1000 {
public:
void Method() {
std::cout << "1000 Millis called" << std::endl;
}
};
class CallBack2000 {
public:
void Method() {
std::cout << "2000 Millis called" << std::endl;
}
};
template <uint32_t tMilliSeconds>
class Timer {
private:
static Timer *_instance;
uint32_t mMilliSeconds;
boost::mutex mListMutex;
std::vector<boost::function<void()> > mHandlerList;
Timer() {
mMilliSeconds = tMilliSeconds;
}
Timer(const Timer &other) {}
Timer &operator=(const Timer &other) {}
public:
static Timer *Instance() {
if (_instance == NULL) {
_instance = new Timer();
}
return _instance;
}
void Run() {
while(true) {
boost::this_thread::sleep(
boost::posix_time::milliseconds(mMilliSeconds));
mListMutex.lock();
for (std::vector<boost::function<void()> >::iterator vect_it =
mHandlerList.begin(); vect_it != mHandlerList.end();
++vect_it) {
(*vect_it)();
}
mListMutex.unlock();
}
}
void AddHandler(boost::function<void()> tmpBoostFunction) {
mListMutex.lock();
mHandlerList.push_back(tmpBoostFunction);
mListMutex.unlock();
}
};
template<> Timer<1000> *Timer<1000>::_instance = NULL;
template<> Timer<2000> *Timer<2000>::_instance = NULL;
int main(int /*argc*/, char ** /*argv*/) {
CallBack1000 tmpCB1000;
boost::shared_ptr<Timer<1000> > mPtrTimer1000(Timer<1000>::Instance());
mPtrTimer1000->AddHandler(boost::bind(&CallBack1000::Method, tmpCB1000));
CallBack2000 tmpCB2000;
boost::shared_ptr<Timer<2000> > mPtrTimer2000(Timer<2000>::Instance());
mPtrTimer2000->AddHandler(boost::bind(&CallBack2000::Method, tmpCB2000));
boost::thread tmpThread1000(
boost::bind(&Timer<1000>::Run, mPtrTimer1000));
boost::thread tmpThread2000(
boost::bind(&Timer<2000>::Run, mPtrTimer2000));
tmpThread1000.join();
tmpThread2000.join();
}
EDIT
#include <boost/shared_ptr.hpp>
#include "singletontimer.h"
#include <assert.h>
class CallBack100 {
public:
void Method() {
std::cout << "100 Millis called" << std::endl;
}
};
class CallBack20 {
public:
void Method() {
std::cout << "20 Millis called" << std::endl;
}
};
int main(int /*argc*/, char ** /*argv*/) {
boost::shared_ptr<Timer<20> > tmpPtrTimer_20 = Timer<20>::Instance();
boost::shared_ptr<Timer<100> > tmpPtrTimer_100 = Timer<100>::Instance();
boost::shared_ptr<Timer<20> > tmpPtrTimer2_20 = Timer<20>::Instance();
CallBack20 tmpCallBack1;
tmpPtrTimer_20->AddHandler(boost::bind(&CallBack20::Method, tmpCallBack1));
CallBack20 tmpCallBack2;
tmpPtrTimer2_20->AddHandler(boost::bind(&CallBack20::Method, tmpCallBack2));
CallBack100 tmpCallBack3;
tmpPtrTimer_100->AddHandler(boost::bind(&CallBack100::Method, tmpCallBack3));
assert(tmpPtrTimer_20 == tmpPtrTimer2_20 && "Smart Pointers do Not Equal");
while(true) {
boost::this_thread::sleep(
boost::posix_time::milliseconds(100000));
}
}
#ifndef TIMER_H
#define TIMER_H
#include <boost/bind/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <stdint.h>
#include <iostream>
#include <vector>
template <uint32_t tMilliSeconds>
class Timer {
private:
static Timer *_instance;
uint32_t mMilliSeconds;
boost::recursive_mutex mListMutex;
boost::thread mTimerThread;
std::vector<boost::function<void()> > mHandlerList;
Timer();
Timer(const Timer &other);
Timer &operator=(const Timer &other);
public:
static boost::shared_ptr<Timer<tMilliSeconds> > Instance();
void Run();
void AddHandler(boost::function<void()> tmpBoostFunction);
};
template <uint32_t tMilliSeconds>
Timer<tMilliSeconds>::Timer() {
mMilliSeconds = tMilliSeconds;
mTimerThread = boost::thread(
boost::bind(&Timer<tMilliSeconds>::Run, this));
}
template <uint32_t tMilliSeconds>
boost::shared_ptr<Timer<tMilliSeconds> >
Timer<tMilliSeconds>::Instance() {
if (!_instance) {
_instance = new Timer<tMilliSeconds>();
}
return boost::shared_ptr<Timer<tMilliSeconds> >(_instance);
}
template <uint32_t tMilliSeconds>
void Timer<tMilliSeconds>::Run() {
while(true) {
boost::this_thread::sleep(
boost::posix_time::milliseconds(mMilliSeconds));
boost::lock_guard<boost::recursive_mutex> tmpLockGuard(mListMutex);
for (std::vector<boost::function<void()> >::iterator vect_it =
mHandlerList.begin(); vect_it != mHandlerList.end();
++vect_it) {
(*vect_it)();
}
}
}
template <uint32_t tMilliSeconds>
void Timer<tMilliSeconds>::AddHandler(
boost::function<void()> tmpBoostFunction) {
boost::lock_guard<boost::recursive_mutex> tmpLockGuard(mListMutex);
mHandlerList.push_back(tmpBoostFunction);
}
#endif // TIMER_H
#include "singletontimer.h"
template<> Timer<20> *Timer<20>::_instance = NULL;
template<> Timer<100> *Timer<100>::_instance = NULL;