簡體   English   中英

如何創建“工作”線程以使用多核處理器的強大功能?

[英]how to create “worker” threads to use the power of multi-core processors?

這是我之前的問題Paralleling交易軟件的修訂版

代碼是:

foreach (Strategy strategy in strategies)
{
    strategy.AllTablesUpdated();
    Console.WriteLine("All tables updated in " + sw.ElapsedMilliseconds);
}

...........

public override void AllTablesUpdated()
{
    lock (desiredOrdersBuy)
    ....

輸出是:

All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 0
All tables updated in 9
All tables updated in 9
All tables updated in 9
All tables updated in 9
All tables updated in 23
All tables updated in 35
All tables updated in 35
All tables updated in 35
All tables updated in 35
All tables updated in 35
All tables updated in 35
All tables updated in 43
All tables updated in 43
All tables updated in 43
All tables updated in 44
All tables updated in 44
All tables updated in 44
All tables updated in 44
All tables updated in 44
All tables updated in 58
All tables updated in 65
All tables updated in 65
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 78
All tables updated in 87
All tables updated in 95
All tables updated in 111
All tables updated in 111
All tables updated in 111
All tables updated in 136
All tables updated in 153
All tables updated in 153
All tables updated in 171
All tables updated in 171
All tables updated in 178
All tables updated in 178
All tables updated in 178
All tables updated in 178
All tables updated in 178
All tables updated in 178
All tables updated in 178

問題是 - 當某些策略等待鎖定被釋放時,其他策略也會等待。

我想我應該用Monitor.TryEnter替換lockAllTablesUpdated應該返回boolean (如果因為沒有獲得鎖而需要重新啟動,則為false)。

另外我認為我需要4-6個並行線程來執行“AllTablesUpdated”方法來使用多核處理器的強大功能。 如何創建這些線程?

您可以使用Parallel.ForEach在單獨的線程中對每個策略對象執行某些操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM