Previous: A Distributed List Up: Examples

Producer and Consumer

Even though the global pointer is not necessary for the list presented above if the appender and remover are in the same processor object, it will still function correctly. We can see it working with this program, gptr_prod_cons.cc++


#include <iostream.h>
#include "gptr_dlist.h"

class Consumer { public: DList_removing* remover; Consumer() { remover = new DList_removing(); } Consumer() { delete remover; }

void consume (int n) { for (int i=0; i<n; i++) cout << "Consumer removes: " << remover->remove() << endl; } };

class Producer { public: DList_appending* appender;

Producer(DList_removing *global remover) { appender = new DList_appending(remover); }

Producer() { delete appender; }

void produce(int n) { for (int i=0; i<n; i++) { cout << "[appending " << i << "]"; appender->append(i); } } };

int main (int argc, char**argv) { Consumer C; Producer P(C.remover); par { P.produce(10); C.consume(10); } return 0; }

We compile and run as follows:


>cc++ gptr_dlist.cc++ -c
>cc++ gptr_prod_cons.cc++ -o gptr_prod_cons.out gptr_dlist.o
>pvmd &
>dpc.out

In Chapter , after we have seen how to create processor objects, we use this distributed list class across processor objects.

paolo@cs.caltech.edu