Answers/Solutions to Exercises in Chapter 11, Exercise 6

E6: Write a simple multithreaded C++ program using smart pointers that are "thread aware", i.e. the deallocation only takes place in
the thread that is the "owner".

S6: Se the sample program below. The first thread created will delete two objects, the a local in doit1()when it goes out of scope, and the global p it created, the second thread will delete the object a local in doit1() when it goes out of scope, and the will "pass" through the object p since it did not create it. The critical sections are there for the same reasons as in Exercise 5. The makefile would be the same as in Exercise 5.

extern "C" {
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
}
 #include "log.h"


//variables for indexing of messages by logging functions
int logindex=0;
int *logi = &logindex;
//thread mutex lock for access to the log
pthread_mutex_t tlock = PTHREAD_MUTEX_INITIALIZER; 
//thread mutex lock for critical sections
pthread_mutex_t tlock1 = PTHREAD_MUTEX_INITIALIZER; 


void* doit(void*);
void doit1();
pthread_t tid1, tid2;

class OBJECT
{
 public:
  OBJECT(char* s) { 
    p=new char[strlen(s)+1];
    strcpy(p,s);
    threadid=pthread_self();
  }
  ~OBJECT() { Delete(); }
  void Delete() { 
    if (pthread_self()==threadid) {
      Msg("thread %u deleting object %x belonging to %d",
          pthread_self(),this,threadid);
      delete[] p;
    }else{
      Msg("thread %u not deleting object %x belonging to %d",
          pthread_self(),this,threadid);
    }
  }
 protected:
  char* p;
  pthread_t threadid;
};

OBJECT* p=0;


// function main  ------------------------------------------------- 
int main()
{
  create_log("log.txt");

  Msg("going to create the first thread");
  pthread_create(&tid1,NULL,doit,NULL);
  
  Msg("going to create the second thread");
  pthread_create(&tid2,NULL,doit,NULL);
  
  Msg("going to wait for the first thread to exit");
  pthread_join(tid1,NULL);
  Msg("the first thread exited");

  Msg("going to wait for the second thread to exit");
  pthread_join(tid2,NULL);
  Msg("the second thread exited");

  exit(0);

}//end main



// function doit -------------------------------------------------- 
void* doit(void* x)
{
  doit1();
  pthread_exit(NULL);
  return NULL;

}//end doit


// function doit1 -----------------------------------------------------
void doit1()
{
  pthread_t me;
  char buf[100];

  me = pthread_self();
  Msg("I am thread %u",me);
  sprintf(buf,"thread=%u",me);

  OBJECT a(buf);

   pthread_mutex_lock(&tlock1); // critical section starts
   if (p==0)
     p = new OBJECT("hello");
   pthread_mutex_unlock(&tlock1);  // critical section ends

    if (me==tid1) sleep(1);

   pthread_mutex_lock(&tlock1); // critical section starts
   if (p!=0)
     p->Delete();
   pthread_mutex_unlock(&tlock1);  // critical section ends

}//end doit1

output in log:

message number = 0, process id = 21173, time and date = 08:45:08 05/08/04
 going to create the first thread
message number = 1, process id = 21173, time and date = 08:45:08 05/08/04
 going to create the second thread
message number = 2, process id = 21173, time and date = 08:45:08 05/08/04
 I am thread 4
message number = 3, process id = 21173, time and date = 08:45:08 05/08/04
 going to wait for the first thread to exit
message number = 4, process id = 21173, time and date = 08:45:08 05/08/04
 I am thread 5
message number = 5, process id = 21173, time and date = 08:45:08 05/08/04
 thread 5 not deleting object 26880 belonging to 4
message number = 6, process id = 21173, time and date = 08:45:08 05/08/04
 thread 5 deleting object fe5ffc40 belonging to 5
message number = 7, process id = 21173, time and date = 08:45:09 05/08/04
 thread 4 deleting object 26880 belonging to 4
message number = 8, process id = 21173, time and date = 08:45:09 05/08/04
 thread 4 deleting object fe77fc40 belonging to 4
message number = 9, process id = 21173, time and date = 08:45:09 05/08/04
 the first thread exited
message number = 10, process id = 21173, time and date = 08:45:09 05/08/04
 going to wait for the second thread to exit
message number = 11, process id = 21173, time and date = 08:45:09 05/08/04
 the second thread exited

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 11 Index