使用c++实现时间轮算法(Timing-Wh…

      前段时间写了篇blog描述了时间轮算法(http://blog.sina.com.cn/s/blog_48d4cf2d0100pq9o.html),昨天终于动手用c++将其实现了,并通过了单元测试。我没有看Linux内核的实现代码,只是看了资料凭自己的理解来实现,在效率方面也没有尽量优化。所以下面的代码用来帮助理解算法可以,拿来实际用就不一定靠谱了。
      另外我没有实现删除函数,因为懒得自己实现链表嘿嘿。参考:http://blog.csdn.net/lanmanck/archive/2009/09/07/4528759.aspx。删除的做法很简单,setTimer时直接返回链表结点,删除的时候干掉自己即可。
===========================timing_wheel.h================================
#pragma once

#include <list>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>

namespace global
{
      typedef unsigned int uint;

      class TimeoutHandler
      {
      public:
            virtual ~TimeoutHandler(){}
            virtual void onTimeout() = 0;
      };
     
      class TimingWheel
      {
      public:
            TimingWheel();
            ~TimingWheel();

            void setTimer(uint inteval, boost::shared_ptr<TimeoutHandler> handler);
            void step();

      private:

            typedef boost::shared_ptr<TimeoutHandler> Handler;
            struct ListNode
            {
                  uint inteval;
                  Handler handler;
                  ListNode(int inteval_, Handler handler_)
                        : inteval_r(inteval_)
                        , handler(handler_)
                  {
                  }
            };

            enum {BUCKET_CNT = 5};
            uint newest[BUCKET_CNT];
            std::list<ListNode>* buckets[BUCKET_CNT];
            boost::recursive_mutex mtx;
      };

}
===========================timing_wheel.cpp================================
#include "timing_wheel.h"
#include "xassert.h"
#include <stdexcept>
#include <vector>

namespace global
{
      static const uint ELEMENT_CNT_PER_BUCKET[] = {256, 64, 64, 64, 64};
      static const uint RIGHT_SHIFT_PER_BUCKET[] = {8, 6, 6, 6, 6};
      static const uint BASE_PER_BUCKET[] = {1, 256, 256*64, 256*64*64, 256*64*64*64};

      TimingWheel::TimingWheel()
      {
            for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
            {
                  newest[bucket_no] = 0;
                  buckets[bucket_no] = new std::list<ListNode>[ELEMENT_CNT_PER_BUCKET[bucket_no]];
            }
      }

      TimingWheel::~TimingWheel()
      {
            for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
            {
                  delete[] buckets[bucket_no];
            }
      }

      void TimingWheel::setTimer(uint inteval, boost::shared_ptr<TimeoutHandler> handler)
      {
            boost::recursive_mutex::scoped_lock lock(mtx);
            XASSERT(inteval > 0);
            uint bucket_no = 0;
            uint offset = inteval;
            uint left = inteval;
            while (offset >= ELEMENT_CNT_PER_BUCKET[bucket_no])
            {
                  offset >>= RIGHT_SHIFT_PER_BUCKET[bucket_no];
                  left -= BASE_PER_BUCKET[bucket_no]*(ELEMENT_CNT_PER_BUCKET[bucket_no] - newest[bucket_no] - (bucket_no == 0 ? 0 : 1));
                  ++ bucket_no;
            }
            XASSERT(offset >= 1);
            XASSERT(inteval >=  BASE_PER_BUCKET[bucket_no]*offset);
            left -= BASE_PER_BUCKET[bucket_no] * (offset - 1);
            uint pos = (newest[bucket_no] + offset) % ELEMENT_CNT_PER_BUCKET[bucket_no];
            buckets[bucket_no][pos].push_back(ListNode(left, handler));
      }

      void TimingWheel::step()
      {
            std::vector<Handler> handlers;
            {
                  boost::recursive_mutex::scoped_lock lock(mtx);
                  for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
                  {
                        newest[bucket_no] = (newest[bucket_no] + 1) % ELEMENT_CNT_PER_BUCKET[bucket_no];
                        std::list<ListNode>& cur_list(buckets[bucket_no][newest[bucket_no]]);
                        while (!cur_list.empty())
                        {
                              if (bucket_no == 0 || cur_list.front().inteval == 0)
                              {
                                    handlers.push_back(cur_list.front().handler);
                              }
                              else
                              {
                                    setTimer(cur_list.front().inteval, cur_list.front().handler);                                                                       
                              }
                              cur_list.pop_front();                             
                        }
                        if (newest[bucket_no] != 0)
                        {
                              break;
                        }
                  }
            }
            for (std::size_t i = 0; i < handlers.size(); i++)
            {
                  handlers[i]->onTimeout();
            }
      }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值