Sleep Function Error In C

前端 未结 5 1215
眼角桃花
眼角桃花 2021-01-21 03:03

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 t

5条回答
  •  无人及你
    2021-01-21 03:19

    If I understand well:

    • you have a thread that send data (through network ? what is the source of data ?)
    • you slow down sending rythm using sleep
    • the received data (at the other end of network) can be delayed much more (15 s instead of 10s)

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

    • sleep is very imprecise, it will wait at least n seconds, but it may be more (especially if your system is loaded by other running apps).
    • networks introduce a buffering delay, you have no guarantee that your data will be send immediately on the wire (usually it is not).
    • the trip itself introduce some delay (latency), if your protocol wait for ACK from the receiving end you should take that into account.
    • you should also consider time necessary to read/build/retrieve data to send and really send it over the wire. Depending of what you are doing it can be negligible or take several seconds...

    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.

    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.

    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.

    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.

    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.

提交回复
热议问题