chenshuo / muduo

Event-driven network library for multi-threaded Linux server in C++11
https://github.com/chenshuo/muduo
Other
14.7k stars 5.15k forks source link

TimerQueue中两个set问题? #548

Closed cd106224 closed 2 years ago

cd106224 commented 2 years ago

Muduo库在TimerQueue中为什么要设置两个set,我发现 ActiveTimerSet是按地址排序的,注释上写时为了cancel,但是在cancel函数中也可以用TimerList来find进行取消,请问用两个set的初衷是什么 image

chenshuo commented 2 years ago

TimerQueue::cancelInLoop() 中 ActiveTimerSet::find() 的时间复杂度是 O(log N); 如果改用 TimerList::find(),那么时间复杂度会增加到 O(N),因为你不知道 Timerstamp。 你也不能通过 Timer 找到 Timestamp,这里的 Timer 不一定指向合法的对象,因为这个 Timer 对象可能已经析构了。