JezK
Edit File: cdesc-Observable.ri
U:RDoc::NormalModule[iI"Observable:EFI"$Concurrent::Concern::Observable;F0o:RDoc::Markup::Document:@parts[o;;[o:RDoc::Markup::Paragraph;[I"RThe [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) is one ;TI"(of the most useful design patterns.;To:RDoc::Markup::BlankLine o; ;[I"!The workflow is very simple:;To:RDoc::Markup::List: @type:BULLET:@items[ o:RDoc::Markup::ListItem:@label0;[o; ;[I"Dan `observer` can register itself to a `subject` via a callback;To;;0;[o; ;[I"=many `observers` can be registered to the same `subject`;To;;0;[o; ;[I"Lthe `subject` notifies all registered observers when its status changes;To;;0;[o; ;[I"Nan `observer` can deregister itself when is no more interested to receive;To:RDoc::Markup::Verbatim;[I"event notifications ;T:@format0o; ;[ I"JIn a single threaded environment the whole pattern is very easy: the ;TI"L`subject` can use a simple data structure to manage all its subscribed ;TI"P`observer`s and every `observer` can react directly to every event without ;TI""caring about synchronization.;T@o; ;[ I"QIn a multi threaded environment things are more complex. The `subject` must ;TI"Osynchronize the access to its data structure and to do so currently we're ;TI"Vusing two specialized ObserverSet: {Concurrent::Concern::CopyOnWriteObserverSet} ;TI"8and {Concurrent::Concern::CopyOnNotifyObserverSet}.;T@o; ;[I"QWhen implementing and `observer` there's a very important rule to remember: ;TI"P**there are no guarantees about the thread that will execute the callback**;T@o; ;[I"Let's take this example ;TI" ``` ;TI"class Observer;To;;[I"def initialize ;TI" @count = 0 ;TI" end ;TI" ;TI"def update ;TI" @count += 1 ;TI" end ;T;0o; ;[I"end;T@o; ;[ I"obs = Observer.new ;TI"?[obj1, obj2, obj3, obj4].each { |o| o.add_observer(obs) } ;TI"(# execute [obj1, obj2, obj3, obj4] ;TI"```;T@o; ;[I"O`obs` is wrong because the variable `@count` can be accessed by different ;TI"Rthreads at the same time, so it should be synchronized (using either a Mutex ;TI"or an AtomicFixum);T: @fileI")lib/concurrent/concern/observable.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[[ I"observers;TI"RW;T:protectedFI")lib/concurrent/concern/observable.rb;T[ [ [[I" class;T[[:public[ [;[ [:private[ [I" instance;T[[;[ [I"add_observer;F@_[I"count_observers;F@_[I"delete_observer;F@_[I"delete_observers;F@_[I"with_observer;F@_[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@ZI"Concurrent::Concern;FcRDoc::NormalModule