<p>In a digital signal acquisition system, often data is pushed into an observer in the system by one thread.  </p>

<p>example from <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer">Wikipedia/Observer_pattern</a>:</p>

<pre><code>foreach (IObserver observer in observers)
    observer.Update(message);
</code></pre>

<p>When e.g. a user action from e.g. a GUI-thread requires the data to stop flowing, you want to break the subject-observer connection, and even dispose of the observer alltogether.</p>

<p>One may argue: you should just stop the data source, and wait for a sentinel value to dispose of the connection.  But that would incur more latency in the system.</p>

<p>Of course, if the data pumping thread has just asked for the address of the observer, it might find it's sending a message to a destroyed object.</p>

<p>Has someone created an 'official' Design Pattern countering this situation?  Shouldn't they?</p>

[{'answer_id': 82116, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 0, 'selected': False, 'text': '<p>You could send a message to all observers informing them the data source is terminating and let the observers remove themselves from the list.</p>\n\n<p>In response to the comment, the implementation of the subject-observer pattern should allow for dynamic addition / removal of observers. In C#, the event system is a subject/observer pattern where observers are added using <code>event += observer</code> and removed using <code>event -= observer</code>.</p>\n'}, {'answer_id': 102984, 'author': 'QBziZ', 'author_id': 11572, 'author_profile': 'https://Stackoverflow.com/users/11572', 'pm_score': 3, 'selected': True, 'text': "<p>If you want to have the data source to always be on the safe side of concurrency, you should have at least one pointer that is always safe for him to use. \nSo the Observer object should have a lifetime that isn't ended before that of the data source.</p>\n\n<p>This can be done by only adding Observers, but never removing them. \nYou could have each observer not do the core implementation itself, but have it delegate this task to an ObserverImpl object. \nYou lock access to this impl object. This is no big deal, it just means the GUI unsubscriber would be blocked for a little while in case the observer is busy using the ObserverImpl object. If GUI responsiveness would be an issue, you can use some kind of concurrent job-queue mechanism with an unsubscription job pushed onto it. ( like PostMessage in Windows )</p>\n\n<p>When unsubscribing, you just substitute the core implementation for a dummy implementation. Again this operation should grab the lock. This would indeed introduce some waiting for the data source, but since it's just a [ lock - pointer swap - unlock ] you could say that this is fast enough for real-time applications.</p>\n\n<p>If you want to avoid stacking Observer objects that just contain a dummy, you have to do some kind of bookkeeping, but this could boil down to something trivial like an object holding a pointer to the Observer object he needs from the list.</p>\n\n<p>Optimization :\nIf you also keep the implementations ( the real one + the dummy ) alive as long as the Observer itself, you can do this without an actual lock, and use something like InterlockedExchangePointer to swap the pointers.\nWorst case scenario : delegating call is going on while pointer is swapped --> no big deal all objects stay alive and delegating can continue. Next delegating call will be to new implementation object. ( Barring any new swaps of course )</p>\n"}]
['https://Stackoverflow.com/questions/82074', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6610/']