I3MUP1+Exercise+6.1

toc =Exercise 6.1= //This exercise will teach you the basics of threads and mailboxes from the OS API.//

Exercise 6.1.1
//Redo Lab Exercise 5.1 (the sender-receiver exercise) on a PC platform using the win32 edition of the OS API supplied with this exercise.//

We implement two classes called Sender and Receiver that inherit from the provided Thread class. Their respective run functions provide the functionality of the sender and receiver thread functions from exercise 5.1. The run function is a virtual protected function, and is wrapped inside the start function which is used to start a thread. These two thread classes share a mailbox through which they exchange data (a Point3D object).

The bits of code shown below show the syntax for using the provided win23 API to implement threads and mailboxes.

Main
Main simply creates a mailbox able to contain Point3D objects and the two threads, which are then started. code format="cpp" int main { SleepTimer t; Mailbox mb(10);

Sender sender(mb); Receiver receiver(mb);

sender.start; receiver.start;

while(true) t.sleep(1000); } code

Sender
The Sender thread has a timer object and a Point3D object. The point object is sent through the mailbox, and the coordinates of the object are then increased by 1 using an overloaded ++ operator. This is repeated every second in a while loop. code format="cpp" Sender::Sender(Mailbox& mb) : Thread(Thread::PRIORITY_NORMAL, "sender"), mailbox(mb) {

}

void Sender::run { Point3D v; SleepTimer t;

while(1) { mailbox.put(++v); t.sleep(1000); } } code

Receiver
All the receiver thread does is get data from the mailbox, and display that data using an overloaded << operator. code format="cpp" Receiver::Receiver(Mailbox& mb) : Thread(Thread::PRIORITY_NORMAL, "receiver"), mailbox(mb) {

}

void Receiver::run { Point3D v; while(1) { v=mailbox.get; cout << v << endl; } } code