Looking for Thread-Safe Observer Pattern

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm

Looking for Thread-Safe Observer Pattern

Post by iblues1976 » Tue Feb 17, 2015 12:19 am

Hi,

Does anyone has observer pattern, thread-safe that could share?

Thanks
0 x

User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
Contact:

Re: Looking for Thread-Safe Observer Pattern

Post by Klaim » Tue Feb 17, 2015 2:54 pm

I have several implementations for different use case. Do you have a specific use case?

One example of such system would be boost::signals2 : http://www.boost.org/doc/libs/1_57_0/do ... nals2.html

Not the most efficient but easy to use and you can build more complex dispatching mechanisms over it.
0 x

iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm

Re: Looking for Thread-Safe Observer Pattern

Post by iblues1976 » Sun Feb 22, 2015 10:15 pm

Any of your implementations of the observer pattern.


The use case is input device produces an event ... subscriber listen to the even

event E , eventdata D

I have a working prototype but it wasn't designed for threads. I modified it but I wasn't quite happy with it... I'm going to write a new one but I wanted to see if I could use an existing implementation.
thanks
0 x

User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
Contact:

Re: Looking for Thread-Safe Observer Pattern

Post by Klaim » Mon Feb 23, 2015 11:19 am

Did you look at boost signals? It's a pretty common pattern that should solve your problem.

Also, which version of C++ do you use? So I can give you an example that you can actually use.
0 x

User avatar
stealth977
Gnoll
Posts: 638
Joined: Mon Dec 15, 2008 6:14 pm
Location: Istanbul, Turkey

Re: Looking for Thread-Safe Observer Pattern

Post by stealth977 » Mon Feb 23, 2015 12:35 pm

It all depends on your threading style. Do you use step synced threads? Do you use an independent thread for your input? Do your listeners run on a different thread?

You can only use boost::signal if both your listener and input is running on same thread, if they run on different threads boost signal would be a disaster. In that case you would need a message queue instead.

But again, it all depends on your threading style...

Assuming you would want to run input on a separate thread, you would need a message queue implementation where you push your input messages into (with time stamps). How you fetch messages from that queue depends on threading style again. If you have lock steps, then at some point (where your threads sync) you can process and deliver the queue messages to your registered listeners. If you dont have a sync point for your threads, then you would need classic OS style pulls from the queue where the listeners need to query the queue for any message they are interested in...
0 x
Ismail TARIM
Ogitor - Ogre Scene Editor
WWW:http://www.ogitor.org
Repository: https://bitbucket.org/ogitor

User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
Contact:

Re: Looking for Thread-Safe Observer Pattern

Post by Klaim » Mon Feb 23, 2015 12:38 pm

stealth977 wrote: You can only use boost::signal if both your listener and input is running on same thread, if they run on different threads boost signal would be a disaster. In that case you would need a message queue instead.
Not Boost.Signals2 which is thread-safe (the original signals library have been considered deprecated by the community for a long time). But I agree that the practice of working with tasks pushed in work queues on reactions is very good in a lot of cases. For example, I combine both in my current projects.

Here is a simplist (no disconnection) implementation of a signal in C++14 (for brievety): http://coliru.stacked-crooked.com/a/cdd0a741a76d94e9

as stealth977 points, it might not be the best way to do it depending on your use case.
0 x

User avatar
stealth977
Gnoll
Posts: 638
Joined: Mon Dec 15, 2008 6:14 pm
Location: Istanbul, Turkey

Re: Looking for Thread-Safe Observer Pattern

Post by stealth977 » Mon Feb 23, 2015 1:39 pm

By disaster I didnt mean problem with boost::signal. What I meant was the listener will have problems in the invoked function since the invoked function will be called from another thread, you will need to implement explicit locking for any variables/functions used in the invoked function which is not a proper way of doing multi-threading IMO. But again the term multi-threaded does not mean that there is a single way of implementing it, actually its more of a general term and there are infinite different implementations of it...
0 x
Ismail TARIM
Ogitor - Ogre Scene Editor
WWW:http://www.ogitor.org
Repository: https://bitbucket.org/ogitor

User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
Contact:

Re: Looking for Thread-Safe Observer Pattern

Post by Klaim » Mon Feb 23, 2015 2:24 pm

stealth977 wrote:By disaster I didnt mean problem with boost::signal. What I meant was the listener will have problems in the invoked function since the invoked function will be called from another thread, you will need to implement explicit locking for any variables/functions used in the invoked function which is not a proper way of doing multi-threading IMO. But again the term multi-threaded does not mean that there is a single way of implementing it, actually its more of a general term and there are infinite different implementations of it...
Thanks for the clarification.
Inded, as you already pointed the common pattern is ton make sure the callback is protected too. I tend to endup synchonizing everything with work queues and I end up doing

signal.connect(wrap(work_queue, callback)); // make sure the callback is called in the work queue

a lot.
0 x

iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm

Re: Looking for Thread-Safe Observer Pattern

Post by iblues1976 » Thu Mar 12, 2015 7:47 pm

To reply to a previous question in this thread, the use case is for any type of input device that needs to send messages to subscribers that may be on different threads.
0 x

Post Reply