Need microsecond delay in .NET app for throttling UDP multicast transmission rate

后端 未结 6 1672
遇见更好的自我
遇见更好的自我 2020-12-15 12:37

I\'m writing a UDP multicast client/server pair in C# and I need a delay on the order of 50-100 µsec (microseconds) to throttle the server transmission rate. This helps to a

6条回答
  •  清歌不尽
    2020-12-15 12:48

    Very short sleep times are generally best achieved by a CPU spin loop (like the kind you describe). You generally want to avoid using the high-precision timer calls as they can themselves take up time and skew the results. I wouldn't worry too much about CPU pegging on the server for such short wait times.

    I would encapsulate the behavior in a class, as follows:

    • Create a class whose static constructor runs a spin loop for several million iterations and captures how long it takes. This gives you an idea of how long a single loop cycle would take on the underlying hardware.
    • Compute a uS/iteration value that you can use to compute arbitrary sleep times.
    • When asked to sleep for a particular period of time, divide uS to sleep by the uS/iteration value previously computed to identify how many loop iterations to perform.
    • Spin using a while loop until the estimated time elapses.

提交回复
热议问题