我正在计算股票市场指数,这个指数发生了变化,因此我决定将其分开,然后我们决定将其分开。#34; core" (至少24%虚拟可用核心之一)我的服务器(运行2 * E5-2640)导致到目前为止我只有5%的CPU负载,所以我有很多可用的CPU功率。
由于我需要对股市指数变化做出快速反应,我决定使用无锁代码。这是我将在现实生活中使用的例子:
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace TestBlockingCollection2
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = Stopwatch.StartNew();
int i = 0;
Task.Factory.StartNew(() =>
{
while (true)
{
Console.WriteLine("Adding " + (i + 1));
sw = Stopwatch.StartNew();
i++;
Thread.Sleep(1000);
}
}, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() =>
{
int prevI = 0;
while (true)
{
if (prevI < i)
{
sw.Stop();
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("Received " + i + ". Spent " + microseconds + " microseconds.");
prevI = i;
}
}
}, TaskCreationOptions.LongRunning);
while (true)
{
Thread.Sleep(1000);
}
}
}
}
输出:
C:\bin\testBlockingCollection2>TestBlockingCollection2.exe
Adding 1
Received 1. Spent 1 microseconds.
Adding 2
Received 2. Spent 5 microseconds.
Adding 3
Received 3. Spent 2 microseconds.
Adding 4
Received 4. Spent 2 microseconds.
Adding 5
Received 5. Spent 2 microseconds.
Adding 6
Received 6. Spent 4 microseconds.
Adding 7
Received 7. Spent 2 microseconds.
Adding 8
Received 8. Spent 2 microseconds.
Adding 9
Received 9. Spent 5 microseconds.
Adding 10
Received 10. Spent 2 microseconds.
Adding 11
Received 11. Spent 2 microseconds.
Adding 12
Received 12. Spent 2 microseconds.
Adding 13
Received 13. Spent 2 microseconds.
Adding 14
Received 14. Spent 2 microseconds.
Adding 15
Received 15. Spent 2 microseconds.
Adding 16
Received 16. Spent 3 microseconds.
Adding 17
Received 17. Spent 5 microseconds.
Adding 18
Received 18. Spent 2 microseconds.
Adding 19
Received 19. Spent 2 microseconds.
与使用BlockingCollection
what better do we have to asynchronous execute method than BlockingCollection?
我真的想要这个微秒,因为我有很多可用的CPU功率,并且在HFT微秒内很重要。
所以问题很简单 - 你看到我的例子有什么问题吗?我没有写过无锁代码,所以我想我可能会犯愚蠢的错误。我不使用自旋锁,因为我假设原始更改(int / double / decimal)是&#34; atomic&#34;所以在第二个线程中,我总是收到先前值或新值,但没有别的。
即。如果在一个线程中我将int x从100改为555,在第二个线程中我将有100或555,但从不500或155或其他任何东西。