Previous: Examples Up: Examples Next: Producer and Consumer

A Distributed List

Let's modify the synchronizing single-reader single-writer linked list presented in Chapter to append items on one processor object and remove them on another.

We need to separate the list into two objects:

The data that has been appended but not removed will be stored in DList_removing. Thus, we need to transfer an append request to this object. To do this, we need a global pointer, as DList_removing is in a different processor object than DList_appending.

DList_appending contains a member removing_side, which is a global pointer to an object of class DList_removing. The member function append just forwards the request through this global pointer, calling the member function of DList_removing named real_append.

The declaration in the header file gptr_dlist.h is as follows:


class DList_removing;

class DListNode { private: int data; DListNode *sync next; DListNode (int d) { data = d; } friend class DList_removing; };

class DList_removing { private: DListNode* head; DListNode* tail; atomic void real_append (int a); // Called by DList\_appending public: DList_removing (void); int remove (void); friend class DList_appending; };

class DList_appending { private: DList_removing *global removing_side; public: DList_appending(DList_removing *global lr) : removing_side(lr) {} void append (int a); };

The definition in gptr_dlist.cc++ is as follows:


#include "gptr_dlist.h"

DList_removing::DList_removing (void) { head = new DListNode(0); tail = head; }

atomic void DList_removing::real_append (int a) { DListNode* addition = new DListNode(a); tail->next = addition; tail = addition; }

int DList_removing::remove (void) { DListNode* old_head = head; head = head->next; delete old_head; return head->data; }

void DList_appending::append (int a) { // Use RPC to add item to remote list removing_side->real_append(a); }

The member function real_append of object DList_removing is atomic so that several RPCs to DList_removing can be executing concurrently but not be dangerously sharing mutables. This makes this list a single-reader multiple-writer list. The sync next field of ListNode ensures that a real_append and a remove will not be sharing mutable data.

paolo@cs.caltech.edu