| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #include "TimeScheduler.h"
- #include <thread>
- #include <functional>
- #include "Log.h"
- TimeScheduler::TimeScheduler():timer_(io_)
- {
-
- }
- TimeScheduler::~TimeScheduler()
- {
- m_ITimes.clear();
- io_.stop();
- }
- void TimeScheduler::startTimer()
- {
- std::thread([&] {
- timer_.expires_from_now(boost::asio::chrono::seconds(1));
- timer_.async_wait(std::bind(&TimeScheduler::__onTimer, this, std::placeholders::_1));
- io_.run();
- }).detach();
- }
- void TimeScheduler::attachITimer(ITimer *pTimer)
- {
- m_ITimes.emplace_back(pTimer);
- }
- void TimeScheduler::detachITimer(ITimer *pTimer)
- {
- m_ITimes.remove(pTimer);
- }
- void TimeScheduler::__onTimer(const error_code& ec)
- {
- /*static int i = 0;
- i++;
- std::cout << i << std::endl;*/
- for (ITimer* val:m_ITimes)
- {
- if (val)
- val->onTimer();
- }
- timer_.expires_from_now(boost::asio::chrono::seconds(1));
- timer_.async_wait(std::bind(&TimeScheduler::__onTimer, this, std::placeholders::_1));
- }
- TimeScheduler TimeScheduler::instance;
|