// To demonstare dynamic Start /Stop /Kill thread 

#include "systemc.h"

SC_MODULE(intfc) {
   
   // Refinable ports
   sc_inslave<int > inp2;
   sc_outmaster<int > outp2;
   
   sc_pid *pid;
   sc_pid *pid_child;
   
   int tmp_data;
   
   // Ouput data to display & Create Parent thread & kill child thread
   void inter_f() {
      tmp_data = inp2; // Reads in the data from master
      outp2 = tmp_data; // Writes the value to slave
   
      cout << "--------------- start ----------------------------" << endl;
      sc_create_thread(parent_function,pid); // Create Parent thread dynamically
            sc_start_thread(pid); // Start the Parent thread
      if(pid_child && pid_child->sc_get_thread_status()!= killed) 
         sc_kill_thread(pid_child); // Kill the Child thread
      wait();
   }
   
   // Create child thread & kill parent thread
   void parent_function() {
      sc_create_thread(child,pid_child); // Create child thread dynamically
      sc_start_thread(pid_child); // Start the child thread
      cout <<" Inside Parent "<< endl;
      sc_kill_thread(pid); // Kill the Parent thread
   }
   
   // Stop child process
   void child() {
      cout <<" Inside Child " << endl;
      cout <<"---------------- end ----------------------------" << endl;
      sc_stop_thread(pid_child); // Stop child thread
   }
   
   //Constructor to create & initialize an instance & data structures.
   SC_CTOR(intfc) {
      SC_SLAVE(inter_f, inp2); // Slave thread sensitive to input
      tmp_data = 0;
      pid = 0;
      pid_child = 0;
   }
};
   
