Index: /trunk/SophyaLib/SysTools/commander.cc
===================================================================
--- /trunk/SophyaLib/SysTools/commander.cc	(revision 2954)
+++ /trunk/SophyaLib/SysTools/commander.cc	(revision 2955)
@@ -443,6 +443,9 @@
 static Commander* cur_commander = NULL;
 /* --Methode-- */
-//! Default constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
-Commander::Commander()
+/*! 
+  \brief Constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
+  if \b fgsigstop == true , calls ZThread::ActivateExitOnSignal()
+*/
+Commander::Commander(bool fgsigzt)
 {
 system("cp history.pic hisold.pic");
@@ -512,5 +515,6 @@
 usage += "  > thrlist       # List of command execution threads (& as the last character) \n";
 usage += "  > clearthrlist  # Removes finished threads from the list  \n";
-usage += "  > stopthr Id    # Try to stop a given thread (ThrId=id) sending SIGUSR1 \n";
+usage += "  > killthr Id    # Try to stop a given thread (ThrId=id) by sending SIGUSR1 \n";
+usage += "  > cancelthr Id  # Try to cancel a given thread (ThrId=id)  \n";
 usage += "  > waitthr       # Waits until all active threads have finished (join()) \n";
 usage += "  > exec filename # Execute commands from file \n";
@@ -577,4 +581,5 @@
 AddInterpreter(this);
 curcmdi = this;
+if (fgsigzt) ZThread::ActivateExitOnSignal(SIGUSR1);
 }
 
@@ -1878,8 +1883,13 @@
   return(0);
 }
-else if (kw == "stopthr") {
-  if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: stopthr thrid" << endl;  return(0); }
+else if ( (kw == "killthr") || (kw == "cancelthr") )  {
+  if (tokens.size() < 1) { 
+    cout << "Commander::Interpret() Usage: killthr/cancelthr thrid" << endl;  
+    return(0); 
+  }
   uint_8  id = atol(tokens[0].c_str());
-  StopThr(id);
+  bool fgkill = false;
+  if (kw == "killthr")  fgkill = true;
+  StopThr(id, fgkill);
   return (0);
 }
@@ -2026,22 +2036,31 @@
 void Commander::ListThreads()
 {
-  cout << "---- Commander::ListThreads()  List of separate execution threads NThread=" 
-       <<  CmdThrExeList.size() << " ----- " << endl;
+  cout << "--- Commander::ListThreads()- command execution threads NThread=" 
+       <<  CmdThrExeList.size() << " ---" << endl;
   for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 
       tit != CmdThrExeList.end(); tit++) {
     cout << "Id=" << (*tit)->Id(); 
-    if ( (*tit)->IfDone() )  cout << " Finished , Rc= " << (*tit)->getRC();
-    else cout << " Executing";
-    cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens() << " )" << endl; 
-  }
-}
-/* --Methode-- */
-void Commander::StopThr(uint_8 id)
+    if ( (*tit)->IfRunning() )  cout << " Executing";
+    else if ( (*tit)->IfDone() )  cout << " Finished , Rc= " << (*tit)->getRC();
+    else cout << " Stopped/Canceled";
+    cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens().substr(0,35);
+    if ((*tit)->Tokens().length() > 35) cout << "... )" << endl;
+    else cout << " )" << endl;
+  }
+}
+/* --Methode-- */
+void Commander::StopThr(uint_8 id, bool fgkill)
 {
   for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 
       tit != CmdThrExeList.end(); tit++) {
-    if ( ((*tit)->Id() == id) && !((*tit)->IfDone()) ) { 
-      (*tit)->kill(SIGUSR1);  
-      cout << "Commander::StopThr()  Send signal SIGUSR1 to Thread Id= " << id << endl;
+    if ( ((*tit)->Id() == id) && ((*tit)->IfRunning()) ) { 
+      if (fgkill) {
+	(*tit)->kill(SIGUSR1);  
+	cout << "Commander::StopThr()  Send signal SIGUSR1 to Thread Id= " << id << endl;
+      }
+      else {
+	(*tit)->cancel();
+	cout << "Commander::StopThr()  Canceling Thread Id= " << id << endl;
+      }
       return;
     }
@@ -2055,9 +2074,11 @@
   cout << "---- Commander::CleanThrList()  Cleaning thrlist  ----- \n";
   list<CommandExeThr *> thrcopie;
+  int ncl = 0;
   for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 
       tit != CmdThrExeList.end(); tit++) {
-    if ( (*tit)->IfDone() ) { 
+    if ( ((*tit)->IfEnded() || (*tit)->IfStopped()) && (ncl < 3) ) { 
       cout << " Thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << " Cleaned" << endl;
       delete (*tit);
+      ncl++;
     }
     else thrcopie.push_back((*tit));
Index: /trunk/SophyaLib/SysTools/commander.h
===================================================================
--- /trunk/SophyaLib/SysTools/commander.h	(revision 2954)
+++ /trunk/SophyaLib/SysTools/commander.h	(revision 2955)
@@ -71,5 +71,5 @@
   static Commander*	GetInterpreter();
 
-			Commander();
+			Commander(bool fgsigzt=true);
   virtual		~Commander();
   virtual string	Name();
@@ -171,5 +171,5 @@
   //   Gestion des threads d'execution de commandes 
   void          ListThreads();
-  void          StopThr(uint_8 thrid);
+  void          StopThr(uint_8 thrid, bool fgkill=true);
   void          CleanThrList();
   void          WaitThreads();
Index: /trunk/SophyaLib/SysTools/zthread.cc
===================================================================
--- /trunk/SophyaLib/SysTools/zthread.cc	(revision 2954)
+++ /trunk/SophyaLib/SysTools/zthread.cc	(revision 2955)
@@ -47,15 +47,29 @@
 extern "C" {
   void * zthr_run( void * xthr);
-}
-*/
-
+  void zthr_sig_exit(int s);
+}
+*/
+
+static int _RCR_ = 99;
 static void * zthr_run(void * xthr)
 {
   ZThread * thr = (ZThread *)xthr;
-  thr->run();
-  pthread_exit(NULL);
-  return NULL;
-}
-
+  thr->run_p();
+  return &_RCR_;
+}
+
+static void zthr_cleanup(void * xthr)
+{
+  ZThread * thr = (ZThread *)xthr;
+  if(thr->_status == 1) thr->_status = 2;
+  return;
+}
+
+static int _RCS_ = 97;
+static void zthr_sig_exit(int s)
+{
+  printf("zthr_sig_exit(int s=%d)  signal received -> pthread_exit()\n",s);
+  pthread_exit(&_RCS_);
+}
 
 /* ------ Classe ZThread  -------  */
@@ -65,5 +79,5 @@
 ZThread::ZThread(size_t stacksize)
 {
-  _initok = false;
+  _status = 0;
   _ssize = 0;
   _act = NULL;
@@ -82,5 +96,5 @@
 void ZThread::start()
 {
-  if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
+  if ( IfStarted() ) throw ZThreadExc("ZThread::Start() - Already started thread !");
   int rc;
   pthread_attr_t tha;
@@ -91,5 +105,4 @@
   rc = pthread_create(&_thr, &tha, zthr_run, this);
   CheckSt(rc,"ZThread::start() - Pb creating the thread object");     
-  _initok = true;
   setRC(rc);      
 }
@@ -100,5 +113,5 @@
 void ZThread::cancel()
 {
-  if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
+  if ( !IfStarted() ) throw ZThreadExc("ZThread::cancel() - thread not started !");
   int rc = pthread_cancel(_thr);
   CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");  
@@ -111,5 +124,5 @@
 void ZThread::kill(int sig)
 {
-  if (!_initok) throw ZThreadExc("ZThread::kill() - thread not started !");
+  if ( !IfStarted() ) throw ZThreadExc("ZThread::kill() - thread not started !");
   int rc = pthread_kill(_thr, sig);
   CheckSt(rc,"ZThread::kill() - Pb pthread_kill() ");  
@@ -119,9 +132,9 @@
 
 /*!
-  Waits for the thread to terminate. 
+  \brief Waits for the thread to terminate (call pthread_join() )
 */
 void ZThread::join()
 {
-  if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
+  if ( !IfStarted() ) throw ZThreadExc("ZThread::join() - thread not started !");
   void * x;
   int rc = pthread_join(_thr, &x);
@@ -131,6 +144,9 @@
 
 /*!
+  \brief Method which does the actual computation 
   This virtual method can be redefined in the derived class, in order 
-  to perform the actual computation.
+  to perform the actual computation. The default implementation call
+  the action function (if defined by setAction() )
+  
 */
 void ZThread::run()
@@ -139,4 +155,39 @@
   setRC(0);      
   return;
+}
+
+/*!
+  \brief Method called by the function passed to pthread_create().
+  This method sets the clean-up handler, changes the status variable
+  and call the run() method, and pthread_exit() at the end.
+*/
+void ZThread::run_p()
+{
+  _status = 1;
+  pthread_cleanup_push(zthr_cleanup, (void *)this);
+  run();
+  _status = 2;
+  pthread_exit(&(_rc));
+  pthread_cleanup_pop(0);
+  return;
+}
+
+
+void ZThread::ActivateExitOnSignal(int sig)
+{
+  struct sigaction ae, ad;
+
+  ae.sa_handler = zthr_sig_exit;
+  ad.sa_handler = SIG_DFL;
+  memset( &(ae.sa_mask), 0, sizeof(sigset_t) ); 
+  ae.sa_flags = 0;
+  memset( &(ad.sa_mask), 0, sizeof(sigset_t) ); 
+  ad.sa_flags = 0;
+#ifdef  OSF1
+  ae.sa_flags = SA_RESTART;
+  ad.sa_mask = 0;
+#endif
+  printf(" Activating signal %d  handling for threads -> pthread_exit()\n",sig);
+  sigaction(sig, &ae, NULL);
 }
 
Index: /trunk/SophyaLib/SysTools/zthread.h
===================================================================
--- /trunk/SophyaLib/SysTools/zthread.h	(revision 2954)
+++ /trunk/SophyaLib/SysTools/zthread.h	(revision 2955)
@@ -8,4 +8,5 @@
 #include "machdefs.h"
 #include <pthread.h>
+#include <signal.h>
 #include "pexceptions.h"
 
@@ -36,4 +37,13 @@
   virtual void  run();
 
+  //! Returns true if the thread has been created / started
+  bool IfStarted() { return ((_status > 0) ? true : false); }
+  //! Returns true if the thread is running 
+  bool IfRunning() { return ((_status == 1) ? true : false); }
+  //! Returns true if the thread has ended normally 
+  bool IfEnded() { return ((_status == 2) ? true : false); }
+  //! Returns true if the thread has been stopped (killed/canceled) 
+  bool IfStopped() { return ((_status == 3) ? true : false); }
+
   //! Sets the return code for the thread object
   inline void   setRC(int rc) { _rc = rc; }
@@ -41,15 +51,30 @@
   inline int    getRC() { return(_rc);
  }
+  //! Defines the function which is executed by the default run() method
   inline void	setAction(ZThreadAction act, void * usp=NULL)
        {  _act = act;  _usp = usp; }
 
+  //! Call this method to activate the stop/exit handler on signal \b sig
+  static void ActivateExitOnSignal(int sig=SIGUSR1);
 
+  /*!
+    \brief Set/changes the current thread cancelability state 
+    see pthread_setcancelstate()
+    \b st = PTHREAD_CANCEL_ENABLE  or PTHREAD_CANCEL_DISABLE
+  */
   static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) 
        { int ocs; pthread_setcancelstate(st, &ocs); }
   // PTHREAD_CANCEL_ENABLE  ou PTHREAD_CANCEL_DISABLE
+
+  /*!
+    \brief Set/changes the current thread cancelability state 
+    see pthread_setcanceltype()
+    \b ct = PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS
+  */
   static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) 
        { int oct; pthread_setcanceltype(ct, &oct); }
   // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS
 
+  virtual void  run_p();
 
   // ---- Attribute variables ----
@@ -57,5 +82,5 @@
   size_t _ssize;
   int _rc;
-  bool _initok;
+  int _status; // 0 : not started, 1: running, 2: ended, 3:stopped 
 
   ZThreadAction _act;
