Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You can never avoid observers being removed while iterating. The

The observer can even be removed WHILEWHILE you are trying to call its notify()notify() function. Therefore

Therefore I suppose you need a try/catchtry/catch mechanism. The

The lock is to ensure observerset is not changedd while copying the set of observers

 lock(observers)  set<Observer> os = observers.copy();  unlock(observers)  for (Observer o: os) {   try { o.notify() }   catch (Exception e) {   print "notification of "+o+"failed:"+e   }  } 

You can never avoid observers being removed while iterating. The observer can even be removed WHILE you are trying to call its notify() function. Therefore I suppose you need a try/catch mechanism. The lock is to ensure observerset is not changedd while copying the set of observers

lock(observers) set<Observer> os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } } 

You can never avoid observers being removed while iterating.

The observer can even be removed WHILE you are trying to call its notify() function.

Therefore I suppose you need a try/catch mechanism.

The lock is to ensure observerset is not changedd while copying the set of observers

 lock(observers)  set<Observer> os = observers.copy();  unlock(observers)  for (Observer o: os) {   try { o.notify() }   catch (Exception e) {   print "notification of "+o+"failed:"+e   }  } 
added 20 characters in body
Source Link
mbinette
  • 5.1k
  • 3
  • 27
  • 32

You can never avoid observers being removed while iterating. The observer can even be removed WHILE you are trying to call its notify() function. Therefore I suppose you need a try/catch mechanism. The lock is to ensure observerset is not changedd while copying the set of observers

lock(observers) set os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } }

lock(observers) set<Observer> os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } } 

You can never avoid observers being removed while iterating. The observer can even be removed WHILE you are trying to call its notify() function. Therefore I suppose you need a try/catch mechanism. The lock is to ensure observerset is not changedd while copying the set of observers

lock(observers) set os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } }

You can never avoid observers being removed while iterating. The observer can even be removed WHILE you are trying to call its notify() function. Therefore I suppose you need a try/catch mechanism. The lock is to ensure observerset is not changedd while copying the set of observers

lock(observers) set<Observer> os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } } 
Source Link

You can never avoid observers being removed while iterating. The observer can even be removed WHILE you are trying to call its notify() function. Therefore I suppose you need a try/catch mechanism. The lock is to ensure observerset is not changedd while copying the set of observers

lock(observers) set os = observers.copy(); unlock(observers) for (Observer o: os) { try { o.notify() } catch (Exception e) { print "notification of "+o+"failed:"+e } }