This is the revised version of my previous question Paralleling trading software
The code is:
foreach (Strategy strategy in strategies)
{
strategy.AllTablesUpdated();
Console.WriteLine("All tables updated in " + sw.ElapsedMilliseconds);
}
...........
public override void AllTablesUpdated()
{
lock (desiredOrdersBuy)
....
And the output is:
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
The issue is – when certain strategy waits for lock to be release other strategies waits too.
I think I should replace lock with Monitor.TryEnter and AllTablesUpdated should return boolean (false if it need to be relaunched because lock was not obtained).
Also I think I need 4-6 parallel threads to execute “AllTablesUpdated” method to use the power of multicore processors. How to create these threads?
You can use Parallel.ForEach to do something with each of your Strategy-Objects in a seperated Thread