In my program I use the library Nito.AsyncEx ( https://github.com/StephenCleary/AsyncEx ). In my code, I use the AsyncLock class to synchronize actions. There was a need to find out how many active locks there are at the moment, but this class does not have such a property.
In my program, I create several instances of the Worker class and add them to the collection. Further, the program is instructed to perform a certain action by the Worker, with the least number of locks in the queue. Sample Worker code:
public class Worker { public int Id { get; private set; } public int someProperty1 { get; private set; } public int someProperty2 { get; private set; } AsyncLock _lock = new AsyncLock(); public async Task ActionFirst() { using(await _lock.LockAsync()) { // code } } public async Task ActionSecond() { using(await _lock.LockAsync()) { // code } } public void ParallelActionFirst() { //code } public void ParallelActionSecond() { //code } } Workers can perform quite a lot of functions (10+) in parallel, but some of them must be executed sequentially (ActionFirst, ActionSecond). Some of these sets of actions must be performed sequentially:
ActionFirst -> ActionSecond ActionFirst -> ActionFirst ActionSecond -> ActionFirst ActionSecond -> ActionSecond
Workers have an ID and several other properties. Some tasks provide for the selection of a specific worker, but in the case when a specific worker is NOT needed (or most of the workers are suitable for the task) and the ActionFirst or ActionSecond action must be performed, the minimum loaded should be selected.
Is there a solution to this problem using this library or standard means out of the box C #?