Synchronization mechanism for an observable object

前端 未结 3 2163
情深已故
情深已故 2021-02-18 16:59

Let\'s imagine we have to synchronize read/write access to shared resources. Multiple threads will access that resource both in read and writing (most of times for reading, some

3条回答
  •  野性不改
    2021-02-18 17:52

    This sounds like quite the multi-threading pickle. It's quite challenging to work with recursion in this chain-of-events pattern, whilst still avoiding deadlocks. You might want to consider designing around the problem entirely.

    For example, you could make the addition of an operand asynchronous to the raising of the event:

    private readonly BlockingCollection _additions
        = new BlockingCollection();
    
    public void AddNewOperand(Operand operand)
    {
        _additions.Add(operand);
    }
    

    And then have the actual addition happen in a background thread:

    private void ProcessAdditions()
    {
        foreach(var operand in _additions.GetConsumingEnumerable())
        {
            _container.Lock.EnterWriteLock();
            _container.Operands.Add(operand);
            _container.Lock.ExitWriteLock();
        }
    }
    
    public void Initialize()
    {
        var pump = new Thread(ProcessAdditions)
        {
            Name = "Operand Additions Pump"
        };
        pump.Start();
    }
    

    This separation sacrifices some consistency - code running after the add method won't actually know when the add has actually happened and maybe that's a problem for your code. If so, this could be re-written to subscribe to the observation and use a Task to signal when the add completes:

    public Task AddNewOperandAsync(Operand operand)
    {
        var tcs = new TaskCompletionSource();
    
        // Compose an event handler for the completion of this task
        NotifyCollectionChangedEventHandler onChanged = null;
        onChanged = (sender, e) =>
        {
            // Is this the event for the operand we have added?
            if (e.NewItems.Contains(operand))
            {
                // Complete the task.
                tcs.SetCompleted(0);
    
                // Remove the event-handler.
                _container.Operands.CollectionChanged -= onChanged;
            }
        }
    
        // Hook in the handler.
        _container.Operands.CollectionChanged += onChanged;
    
        // Perform the addition.
        _additions.Add(operand);
    
        // Return the task to be awaited.
        return tcs.Task;
    }
    

    The event-handler logic is raised on the background thread pumping the add messages, so there is no possibility of it blocking your foreground threads. If you await the add on the message-pump for the window, the synchronization context is smart enough to schedule the continuation on the message-pump thread as well.

    Whether you go down the Task route or not, this strategy means that you can safely add more operands from an observable event without re-entering any locks.

提交回复
热议问题