开发者

In C++, with the linux api available, is there a way to have an event occur after a time t has passed?

So far I have been using the linux api and boost libraries in this project so ideally I'd like to stick to those.

I am trying to find out how many bytes my server is sending in a second. My plan is to just have a variable which will reset every second that I append the return value of the send() function to every time I send(the return value is the # of bytes sen开发者_Go百科t).

The problem is: how will I know when to stop appending to the variable? I would like some type of interrupt event which would tell me "Hey, it has been a second! Record the bytes sent and reset the variable before appending to it again!" I already have an epoll setup so if I could add some type of file descriptor that I would poll with epoll that would be ideal!

Any and all advice/suggestions are appreciated. Thank you in advance!

P.S: An idea I have is to have a timer thread. It sleeps for a second then when it wakes up it writes to a message queue. Since message queue's can be polled by epoll I would get the event and know. The sleeper thread would just continue looping. Any better ideas?


Set up a SIGALRM handler to terminate the measurement, then call alarm(2) with the time you want to measure for in seconds.


If you use boost then you perhaps should use boost::asio for asynchronous IO. It wraps most things you need into platform-independent interface. It contains timer and timer has expires_at().


As gettimeofday has precision up to microseconds you can use it to grab timestamps after every send and calculate your instant speed. Also one can aggregate number of bytes for roughly one second (you don't need to be precise at all), divide number of bytes by actual number of microseconds elapsed and send it out to subscribers interested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜