Example : SyncQ.C

Classes/Techniques Demonstrated :

SyncQ.C demonstrates the basics of the HPCxx_SyncQ class. It creates several threads and hands them a pointer to a SyncQ variable. Each thread sleeps for a few seconds, writes a value to the variable and then quits. Meanwhile, the main program is in a loop attempting to read the SyncQ variable. Each time a thread writes to the variable the main program is released to deliver the value received to you, who need it so.
 
class MyThread: public HPCxx_Thread{
   HPCxx_SyncQ<int> *sync;
   int my_num;
   public:
      MyThread( HPCxx_SyncQ *s , int m ):  HPCxx_Thread(){
        my_num = m;
        sync = s;
        cout << "constructor complete" << endl;
      }
      void run(){
         cout << "thread " << my_num << " sleeping..." << endl;
         sleep((10-my_num));
         *sync=my_num;
         cout << "thread " << my_num << " inserted value : " << my_num << endl;
      }
};

int foo(int n) {
   HPCxx_SyncQ<int> s;
   MyThread *t[100];
   int temp;
  
   for(int i = 0; i < n; i++){
     t[i] = new MyThread(&s, i);
     t[i]->start();
   }
   for(i=0; i<n; i++) {
     temp=s;
     cout << "foo() recevied value " << temp << endl;
   }
   return 0;
}

int main(int argc, char *argv[]){
   HPCxx_Group *g;
   hpcxx_init(argc, argv, g);
   int x;
   cout << "num threads:" ;
   cin >> x;
   cout << "doing " << x << "threads.";
   foo(x);
   printf("done with read\n");
   printf("DONE\n");
   hpcxx_exit(g);
   return(0);
} /* main() */
    

hpc++@extreme.indiana.edu

Last modified: Thu Apr 22 02:12:19 EST 1999