Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded Observer Pattern [closed]

I have a question where a subject is updated in a different thread every time. So whenever the subject is updated it correspondingly updates the observer with the new information. However, if the list of observers is long, it will require some time to update all the observers. Think of a subject that gets updated very frequently. While the subject is updating the observers, "subject" object is locked and hence cannot be updated by a different thread. This will either create information traffic for subject or cause loss of information.

Do you have any idea how these issues are handled in a multi-threaded environment? Also, Can anyone recommend some books on parallel programming with C++?

like image 718
shiv chawla Avatar asked Jan 05 '12 18:01

shiv chawla


People also ask

How does the observer pattern implement the Open Closed Principle?

The observer pattern allows for the Open Closed principle. This principle states that a class should be open for extensions without the need to change the class. Using the observer pattern a subject can register an unlimited number of observers.

What is the disadvantage of observer design pattern?

Disadvantages of Observer Design Pattern The main disadvantage of the observer design pattern that subscribers are notified in random order. There is also a memory leakage problem in the observer design pattern because of the observer's explicit register and unregistering.

What is an observer pattern give an example?

Observer is a behavioral design pattern. It specifies communication between objects: observable and observers. An observable is an object which notifies observers about the changes in its state. For example, a news agency can notify channels when it receives news.

What happens in Observer design pattern?

In software design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.


1 Answers

Consider the use of producer-consumer queues or message queues. For your example, you can use a queue in two ways:

  1. Changes to the Subject are queued. When something updates the subject, it puts the new state in the queue and returns immediately. This way, the updater does not block while the observers are notified. You will need a thread that continuously dequeues state changes and updates observers.

  2. Notifications to Observers are queued. Each observer has a queue where subject state-change notifications are posted.

If you are using the Qt library, you can use the signals & slots mechanism with the Qt::QueuedConnection connection type. The slot goes through the receiver's event queue and is executed in the receiver's thread. This way, the sender does not block while the receivers execute their respective slots.

Your program might be a good candidate for the Actor model (paradigm). Here are some C++ libraries that implement the actor model:

  • Theron
  • libcppa (C++11 based)
  • Asynchronous Agents Library (Microsoft)

Your program might also be a good candidate for the Dataflow paradigm. Check out the proposed Boost Dataflow library, which supports threading.


I don't have a book to recommend, but check out Herb Sutter's series of Dr Dobbs articles on C++ concurrency.

like image 101
Emile Cormier Avatar answered Sep 24 '22 01:09

Emile Cormier