我想使用 select() 接收来自其他服务器的更新并定期发送消息。考虑以下设置:
while(1){ select(... timeout = 5 seconds); // some other code}
如果我在t = 2秒收到更新,则select()将返回并执行相应的语句。当下一个循环开始时,超时将再次设置为 5 秒。但是,它应该是 5 - 2 = 3 秒。有没有办法以正确的时间更新计时器?
我想在 select() 之前手动启动一个计时器,但是这个计时器可能与 select() 中使用的计时器不同步。并会导致其他潜在问题。
最佳答案
根据select
man page :
On Linux,
select()
modifies timeout to reflect the amount of time not slept; most other implementations do not do this. (POSIX.1-2001 permits either behaviour.)
因此,您只需重复使用 timeout
变量即可。只有在真正超时时才重置其值。
正如警告所示,依赖此行为会导致移植问题,因此,如果您依赖此行为,请确保对其进行记录,以便在移植代码时完成正确的操作。
关于c++ - 在socket编程的select()中重新启动定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16181571/