Code Monkey home page Code Monkey logo

Comments (6)

SoxSokko avatar SoxSokko commented on May 19, 2024

void CTimer::_AddTimer(uint32_t interval, const base::CMemSharePtr& t, uint64_t& id) {
_time.Now();
uint64_t nowtime = _time.GetMsec();
uint64_t key = nowtime + interval;

std::unique_lock<std::recursive_mutex> lock(_mutex);
while (_timer_map.count(key)) {
    key++;
}
_timer_map[key] = t;
id = key;

}

请问这边这个是采用时间轮的吗,如果定时任务很多,那么内存占用会不会特别大呢,尤其是这边以ms作为刻度?

from cppnet.

caozhiyi avatar caozhiyi commented on May 19, 2024

这个不是时间轮。这个定时器实现的比较粗糙,底层的结构用的是std::map的红黑树,key存储的是超时时的时间戳,如果定时器key冲突,则将key++,这样会造成一些时间上的偏差。
第二个问题,定时任务很多,那么内存占用会不会特别大呢?这里每添加一个定时器只会在红黑树内加一个智能指针和key的节点,内存增长应该也还在接受范围内。
当初在设计的时候考虑TCP网络库并不需要特别多的定时器任务,所以刻度上比较粗,当然应用层也很难获取精确的时间粒度。在HUDP库的定时器实现上,因为消息定时重传,定时器会比较多,在定时器实现上将相同key的定时器放到了链表里。其实这两种方式类似解决hash冲突的方式。

from cppnet.

SoxSokko avatar SoxSokko commented on May 19, 2024

非常感谢您的耐心解答!!!!😁

from cppnet.

wangdassye avatar wangdassye commented on May 19, 2024

用example写了一个简单的计算服务,http请求,当返回数据量较大时,下一次请求服务端会没有响应,客户端取消请求后,在下下次又可以收到客户端请求。这个是哪里出现的问题

from cppnet.

caozhiyi avatar caozhiyi commented on May 19, 2024

用example写了一个简单的计算服务,http请求,当返回数据量较大时,下一次请求服务端会没有响应,客户端取消请求后,在下下次又可以收到客户端请求。这个是哪里出现的问题

光听描述好像定位不出来问题,你方便加下微信吗,或者你fork下然后把你异常的代码上传一下,我看下代码

from cppnet.

wangdassye avatar wangdassye commented on May 19, 2024

用示例写了一个简单的计算服务,http请求,当返回数据量较大时,下一次请求服务端会没有响应,客户端取消请求后,在下下次又可以收到客户端请求。这个是哪里出现的问题

光听描述好像定位不出来问题,你方便加下微信吗,或者你叉下然后把你异常的代码上传一下,我看下代码

我微信号 Wj2976436572

from cppnet.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.