我正在写一个UDP多播客户端/服务器对在C#中,我需要50-100微秒(微秒)量级的延迟扼杀服务器的传输速率。这有助于避免显著分组丢失,也有助于保持从超载是磁盘I / O绑定的客户端。请不要建议Thread.sleep代码或Thread.SpinWait。如果我需要或者那些我不会问。
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 avoid significant packet loss and also helps to keep from overloading the clients that are disk I/O bound. Please do not suggest Thread.Sleep or Thread.SpinWait. I would not ask if I needed either of those.
我首先想到的是使用某种类型的高性能计数器,并做了简单的while()循环检查经过的时间,但我想避免这种情况,因为它感觉kludgey。会不会也盯住了CPU利用率,服务器进程?
My first thought was to use some kind of a high-performance counter and do a simple while() loop checking the elapsed time but I'd like to avoid that as it feels kludgey. Wouldn't that also peg the CPU utilization for the server process?
积分为一个跨平台的解决方案,即不特定的Windows。在此先感谢,伙计们!
Bonus points for a cross-platform solution, i.e. not Windows specific. Thanks in advance, guys!
我会用的秒表但需要一个循环
I would use stopwatch but would need a loop
读这个,以更多的扩展名添加到秒表,就像ElapsedMicroseconds
read this to add more extension to the stopwatch, like ElapsedMicroseconds
之类的东西,这可能工作太
or something like this might work too
System.Diagnostics.Stopwatch.IsHighResolution 必须真
System.Diagnostics.Stopwatch.IsHighResolution MUST be true
static void Main(string[] args)
{
Stopwatch sw;
sw = Stopwatch.StartNew();
int i = 0;
while (sw.ElapsedMilliseconds <= 5000)
{
if (sw.Elapsed.Ticks % 100 == 0)
{ i++; /* do something*/ }
}
sw.Stop();
}