睡眠功能错误在C [英] Sleep Function Error In C

查看:114
本文介绍了睡眠功能错误在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据转储的文件,在不同的时间戳数据可用,我从时间戳的时间和睡觉我的C线程在这个时间。但问题是,实际的时间差为10的第二和我接收在接收端的数据是几乎​​14,15秒的延迟。我使用的窗口操作系统。请指导我。

I have a file of data Dump, in with different timestamped data available, I get the time from timestamp and sleep my c thread for that time. But the problem is that The actual time difference is 10 second and the data which I receive at the receiving end is almost 14, 15 second delay. I am using window OS. Kindly guide me.

对不起,我周的英语。

Sorry for my week English.

推荐答案

如果我的理解得好:


  • 您有发送数据线程(通过网络?是什么数据源?)

  • 您慢下来使用的睡眠节奏发送

  • 所接收的数据(在网络的另一端)可以被延迟多(15秒,而不是10秒)

如果上面的介绍,你在做什么,你的设计有几个缺陷:

If the above describe what you are doing, your design has several flaws:


  • 睡得很IM precise,它会等待至少n秒,但它可能是更(尤其是如果您的系统被其他正在运行的应用加载)。

  • 网络引入缓冲延迟,你有没有保证您的数据将被立即发送上线(通常不需要)。

  • 本身导致的延时(延迟)行了,如果你的协议等待ACK从接收端,你应该考虑到这一点。

  • 您也应该考虑必要的时间来读/生成/检索数据的发送和真正发送过来的电线。根据你在做什么,它可以忽略不计,或采取几秒钟......

如果你给一些更多的细节将会更容易诊断问题的根源。 睡眠你相信(这的确是一个真正的穷人定时器)或系统的其它部分。

If you give some more details it will be easier to diagnostic the source of the problem. sleep as you believe (it is indeed a really poor timer) or some other part of your system.

如果您的转储很大,我会打赌,更多的时间来读取数据和发送过来的电线。你应该在mesure发送过程中消耗的时间(阅读时间前,发送完之后)。

If your dump is large, I will bet that the additional time comes from reading data and sending it over the wire. You should mesure time consumed in the sending process (reading time before and after finishing sending).

如果这确实是额外的时间源,你只需要在接下来的时间等待删除时间。

If this is indeed the source of the additional time, you just have to remove that time from the next time to wait.

范例:发送数据的previous块拿了4S,下一个块为10秒过去了,但你消耗的媒体链接4S,你只是等待6秒。

Example: Sending the previous block of data took 4s, the next block is 10s later, but as you allready consumed 4s, you just wait for 6s.

睡眠仍然是一个相当IM​​ precise计时器,显然,如果发送时间比sendings之间的延迟越大,上述机制将无法正常工作,但你的主意。

sleep is still a quite imprecise timer and obviously the above mechanism won't work if sending time is larger than delay between sendings, but you get the idea.

校正睡眠不那么坏在Windows环境下,因为它是在Unix系统。窗户睡觉的精度为毫秒,UNIX睡眠精度为秒。如果你并不需要太高的precision时间(如果网络涉及高precision时间超出反正达到)睡眠应该没问题。

Correction sleep is not so bad in windows environment as it is in unixes. Accuracy of windows sleep is millisecond, accuracy of unix sleep is second. If you do not need high precision timing (and if network is involved high precision timing is out of reach anyway) sleep should be ok.

这篇关于睡眠功能错误在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆