Pages

Wednesday, November 14, 2012

ISU - Exercise 7 - Thread Communication


Goal

Learning the basics of thread communication using the message queue concept. 

1. Creating a message queue

In this exercise we create our message class. This class is the most basic class and all other classes will inherient from this class. 

We make the destructor virtual because we want to use the inheriented class' destrcutor so that if we create new variables in the inheriented class that variable will also be removed.

Why is this last bit very important? Explain!
The last bit is very important because, you have to destruct the specific constructed class and not the basis class. Without the virtual  destructor in the basis class, you would not have the possibility to enable/acces an inherited destructor through your basis class pointer.

We use the STL template Queue. It uses af FIFO concept and is therfor ideal in our situation where we have a message queue


Files:
Message.h
MsqQueue.h

Code solution
File: Message.h

File: MsqQueue.h


2. Sending data from one thread to another

Create a function main() that creates a message queue and then spawns the two said threads running Sender and Receiver respectively that communicate via this message queue.

Files:
main.cpp


Code solution
File: main.cpp


Questions to answer:
       Who is supposed to be responsible for disposing of the incoming messages?
The receiver is responsible for disposing the incoming message because only it knows when it is done using it.

       Who should be the owner of the object instance of class MsgQueue; Is it relevant in this 
particular scenario?
Main should be the owner of the object instance of class MsgQueue because both the Sender and Receiver thread has to have a pointer to the same Message queue.

       How are the threads brought to know about the object instance of MsgQueue?
When they are created they both get a pointer to the same message queue.

       In the chosen design struct Point3D inherits from class Message. What is the alternative to inheritance?
The alternative for inheritance is either to make a template based class, void* or a simple array of bytes.

We tested the program and the receiver gets what the sender sends.

3. Enhancing the PCLS with Message Queue

Diagram:
Since we have more threads and many function a state diagram can seem overwhelming. Therefor we decided to create a sequence diagram.






















Questions to answer:
       What is an event driven system?
An event driven system is driven by events. An event driven system can be described with state diagrams. 

       How and where do you start this event driven system? (remember it is purely reactive!)
The system is started when the first car thread request permission to the entry guard.

       Explain your design choice in the specific situation where a given car is parked inside the carpark and waiting before leaving. Specifically how is the waiting situation handled?
Like our previous solution a car thread waits inside the parking lot by using the sleep function.

       Compare the original Mutex/Conditional solution to the Message Queue solution.
In which ways do they resemble each other? Consider stepwise what happens in the original code and what happens in your new implementation based on Message 
Queues.
They are mostly the same except the Mutex/Conditional variables are in the Message Queue instead of having it directly in the main code.

What do you consider to be the most important benefit achieved by using the EDP 
approach, elaborate.
When implemented it gives much better code. You don't have to specifically lock and unlock mutexes by yourself and it makes the code a lot easier to read. It can help prevent locking errors and deadlocks. Furthermore you don't need to know how mutexes works when you can just use the Message Queue system.


Files:
MsgQueue.h
handlers.h
Messages.h
enum.h
main.cpp

Code solution
File: MsgQueue.h


File: handlers.h


File: Messages.h


File: enum.h


File: main.cpp











1 comment:

  1. The important thing in this exercise:
    Learning what a message queue is and how to use it.
    Get an idea of what eventbased programming is about.
    Handle protection of the messages with conditionals.

    You have done a great job with this exercise.

    You have good explanations regarding the message queue. You explain fine why it is the receiver who is in charge of deleting messages.

    You have also made a fine sequence diagram. It makes good sense.

    We miss some pictures of the execution of your program. It is a small thing, but nice for the review'er to see it works.

    The rest looks really good and you have good answers to the questions.

    ReplyDelete