Index: trunk/SophyaLib/BaseTools/datatype.cc
===================================================================
--- trunk/SophyaLib/BaseTools/datatype.cc	(revision 3207)
+++ trunk/SophyaLib/BaseTools/datatype.cc	(revision 3208)
@@ -14,22 +14,5 @@
 {
 }
-#if defined(__SGICC__) || defined(__IBMCPP__)
-template <class T>
-string DataTypeInfo<T>::getTypeName()
-{
-  if (typeid(T) == typeid(uint_1)) return("uint_1");
-  else if (typeid(T) == typeid(uint_2)) return("uint_2");
-  else if (typeid(T) == typeid(int_2)) return("int_2");
-  else if (typeid(T) == typeid(int_4)) return("int_4");
-  else if (typeid(T) == typeid(int_8)) return("int_8");
-  else if (typeid(T) == typeid(uint_4)) return("uint_4");
-  else if (typeid(T) == typeid(uint_8)) return("uint_8");
-  else if (typeid(T) == typeid(r_4)) return("r_4");
-  else if (typeid(T) == typeid(r_8)) return("r_8");
-  else if (typeid(T) == typeid(complex<r_4>)) return(" complex<r_4> ");
-  else if (typeid(T) == typeid(complex<r_8>)) return(" complex<r_8> ");
-  else return("???unknowntype???");
-}
-#else
+//! Return the type name of T, as a string, (Example: T=uint_8 --> "uint_8")
 template <class T>
 string DataTypeInfo<T>::getTypeName() { return("???unknowntype???"); }
@@ -37,15 +20,17 @@
 string DataTypeInfo<uint_1>::getTypeName() { return("uint_1"); }
 DECL_TEMP_SPEC
+string DataTypeInfo<int_1>::getTypeName() { return("int_1"); }
+DECL_TEMP_SPEC
 string DataTypeInfo<uint_2>::getTypeName() { return("uint_2"); }
 DECL_TEMP_SPEC
 string DataTypeInfo<int_2>::getTypeName() { return("int_2"); }
 DECL_TEMP_SPEC
+string DataTypeInfo<uint_4>::getTypeName() { return("uint_4"); }
+DECL_TEMP_SPEC
 string DataTypeInfo<int_4>::getTypeName() { return("int_4"); }
 DECL_TEMP_SPEC
+string DataTypeInfo<uint_8>::getTypeName() { return("uint_8"); }
+DECL_TEMP_SPEC
 string DataTypeInfo<int_8>::getTypeName() { return("int_8"); }
-DECL_TEMP_SPEC
-string DataTypeInfo<uint_4>::getTypeName() { return("uint_4"); }
-DECL_TEMP_SPEC
-string DataTypeInfo<uint_8>::getTypeName() { return("uint_8"); }
 DECL_TEMP_SPEC
 string DataTypeInfo<r_4>::getTypeName() { return("r_4"); }
@@ -56,5 +41,41 @@
 DECL_TEMP_SPEC
 string DataTypeInfo< complex<r_8> >::getTypeName() { return(" complex<r_8> "); }
-#endif
+
+/*! 
+  \ brief Return an identifier associated to T 
+  uint_1 -> 1  , int_1 -> 2
+  uint_2 -> 3  , int_2 -> 4
+  uint_4 -> 5  , int_4 -> 6
+  uint_8 -> 7  , int_8 -> 8
+  r_4    -> 9  , r_8   -> 10
+  complex<r_4> -> 11 , complex<r_8> -> 12
+*/
+template <class T>
+int DataTypeInfo<T>::getTypeId() { return(0); }
+DECL_TEMP_SPEC
+int DataTypeInfo<uint_1>::getTypeId() { return(1); }
+DECL_TEMP_SPEC
+int DataTypeInfo<int_1>::getTypeId() { return(2); }
+DECL_TEMP_SPEC
+int DataTypeInfo<uint_2>::getTypeId() { return(3); }
+DECL_TEMP_SPEC
+int DataTypeInfo<int_2>::getTypeId() { return(4); }
+DECL_TEMP_SPEC
+int DataTypeInfo<uint_4>::getTypeId() { return(5); }
+DECL_TEMP_SPEC
+int DataTypeInfo<int_4>::getTypeId() { return(6); }
+DECL_TEMP_SPEC
+int DataTypeInfo<uint_8>::getTypeId() { return(7); }
+DECL_TEMP_SPEC
+int DataTypeInfo<int_8>::getTypeId() { return(8); }
+DECL_TEMP_SPEC
+int DataTypeInfo<r_4>::getTypeId() { return(9); }
+DECL_TEMP_SPEC
+int DataTypeInfo<r_8>::getTypeId() { return(10); }
+DECL_TEMP_SPEC
+int DataTypeInfo< complex<r_4> >::getTypeId() { return(11); }
+DECL_TEMP_SPEC
+int DataTypeInfo< complex<r_8> >::getTypeId() { return(12); }
+
 
 
@@ -115,4 +136,5 @@
 #ifdef __CXX_PRAGMA_TEMPLATES__
 #pragma define_template DataTypeInfo<uint_1>
+#pragma define_template DataTypeInfo<int_1>
 #pragma define_template DataTypeInfo<uint_2>
 #pragma define_template DataTypeInfo<int_2>
@@ -130,4 +152,5 @@
 namespace SOPHYA {
 template class DataTypeInfo<uint_1>;
+template class DataTypeInfo<int_1>;
 template class DataTypeInfo<uint_2>;
 template class DataTypeInfo<int_2>;
Index: trunk/SophyaLib/BaseTools/datatype.h
===================================================================
--- trunk/SophyaLib/BaseTools/datatype.h	(revision 3207)
+++ trunk/SophyaLib/BaseTools/datatype.h	(revision 3208)
@@ -13,8 +13,10 @@
 namespace SOPHYA {
 
+//! class returning type name for usual numrical types (int_2,int_4,r_4,r_8 ...)
 template <class T>
 class DataTypeInfo {
 public:
   static string getTypeName();
+  static int    getTypeId();
   DataTypeInfo();
 };
Index: trunk/SophyaLib/BaseTools/ndatablock.cc
===================================================================
--- trunk/SophyaLib/BaseTools/ndatablock.cc	(revision 3207)
+++ trunk/SophyaLib/BaseTools/ndatablock.cc	(revision 3208)
@@ -32,4 +32,5 @@
 template <class T> size_t NDataBlock<T>::NallocData       = 0;
 template <class T> size_t NDataBlock<T>::NallocSRef       = 0;
+template <class T> ThSafeOp* NDataBlock<T>::gThsop        = NULL;
 
 //! Set debug (and level print) for allocation and references debug.
@@ -88,4 +89,5 @@
 if(Debug_NDataBlock>1)
   cout<<"?_NDataBlock::NDataBlock("<<this<<",n="<<n<<")"<<endl;
+if (gThsop == NULL) gThsop = new ThSafeOp;
 
 Alloc(n, NULL, NULL, fzero);   // allocation et mise a zero
@@ -107,4 +109,5 @@
   cout<<"?_NDataBlock::NDataBlock("<<this
       <<",data="<<data<<",br="<<br<<")"<<endl;
+if (gThsop == NULL) gThsop = new ThSafeOp;
 
 Alloc(n,data,br);
@@ -119,4 +122,5 @@
 if(Debug_NDataBlock>1)
   cout<<"?_NDataBlock::NDataBlock("<<this<<") default"<<endl;
+if (gThsop == NULL) gThsop = new ThSafeOp;
 }
 
@@ -160,5 +164,5 @@
   cout<<"?_NDataBlock::~NDataBlock("<<this<<")"<<endl;
 
-Delete();
+Dealloc();  // ThreadSafe version of Delete()
 }
 
@@ -197,6 +201,10 @@
 if(&a==NULL) throw(NullPtrError("NDataBlock::Share  &a==NULL\n"));
 if(!a.mSRef || a.mSz==0) throw(NullPtrError("NDataBlock::Share a.mSz=0\n"));
+//--- Start of atomic (in one block) operation for thread safety 
+gThsop->lock();   //  (ThreadSafe)
 if(mSRef) Delete();
 mSz = a.mSz; mSRef = a.mSRef; mSRef->nref++;
+gThsop->unlock();   //  (ThreadSafe)
+//--- End of atomic operation 
 
 if(Debug_NDataBlock>1)
@@ -330,4 +338,6 @@
   throw(NullPtrError("NDataBlock::Alloc br!=NULL && data==NULL\n"));
 if(n==0) throw(SzMismatchError("NDataBlock::Alloc n==0\n"));
+//--- Start of atomic (in one block) operation for thread safety (ThreadSafe)
+gThsop->lock();   //  (ThreadSafe)
 if(mSRef) Delete();
 mSz = n;
@@ -338,4 +348,6 @@
 else {mSRef->data = new T[n]; if (zero) memset(mSRef->data,0,n*sizeof(T));}
 mSRef->bridge = br;
+gThsop->unlock();   //  (ThreadSafe)
+//--- End of atomic operation (ThreadSafe)
 
 if(Debug_NDataBlock>0) {
@@ -354,5 +366,5 @@
 }
 
-//! Management of de-allocation
+//! Management of de-allocation (NOT thread-safe)
 template <class T>
 void NDataBlock<T>::Delete(void)
@@ -460,4 +472,17 @@
 }
 
+/*! 
+  \brief Calls the protected Delete() method to set the size to zero
+  This is the public - thread safe version - of the Delete() method
+  The memory is freed if last referenced structure. 
+*/
+template <class T>
+void NDataBlock<T>::Dealloc()
+{
+  gThsop->lock();
+  Delete();
+  gThsop->unlock();
+}
+
 ////////////////
 // Impression //
Index: trunk/SophyaLib/BaseTools/ndatablock.h
===================================================================
--- trunk/SophyaLib/BaseTools/ndatablock.h	(revision 3207)
+++ trunk/SophyaLib/BaseTools/ndatablock.h	(revision 3208)
@@ -10,4 +10,6 @@
 #include "anydataobj.h"
 #include <iostream>
+
+#include "thsafeop.h"      /* For thread safe operation (Ref.Count/Share) */
 
 namespace SOPHYA {
@@ -88,6 +90,6 @@
   void Realloc(size_t nnew,bool force=false);
   
-  //! Calls Delete() to set the size to zero (The memory is freed if last referenced structure)
-  inline void Dealloc() { Delete(); }
+  //  Public, thread safe interface to Delete() (set the size to zero)
+  void Dealloc();
 
   // Informations pointeur/data
@@ -172,4 +174,5 @@
   static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes)
   static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes)
+  static ThSafeOp* gThsop;  //!< DEBUG: for thread safe operation 
 
   size_t       mSz;      //!< size of data structure
Index: trunk/SophyaLib/BaseTools/thsafeop.h
===================================================================
--- trunk/SophyaLib/BaseTools/thsafeop.h	(revision 3208)
+++ trunk/SophyaLib/BaseTools/thsafeop.h	(revision 3208)
@@ -0,0 +1,56 @@
+#ifndef THSAFEOP_H_SEEN
+#define THSAFEOP_H_SEEN
+
+// Classe ThSafeOp : pour permettre l'execution d'operations atomiques en 
+// multithread, lors de gestion de comptage de reference en particulier.
+//  
+// R. Ansari      UPS+LAL IN2P3/CNRS  2007
+
+
+#include "machdefs.h"
+#include "pexceptions.h"
+
+#ifndef SO_NOTHSAFE
+#include <pthread.h>
+#endif
+
+namespace SOPHYA {
+
+
+/*! \brief 
+    Auxiliary class for insuring thread safe operations in SOPHYA, for reference 
+    counting operations 
+*/ 
+class ThSafeOp {
+public:
+#if defined(SO_NOTHSAFE)
+  explicit ThSafeOp() { }
+  ~ThSafeOp() { }
+  inline void	lock() { return; }
+  inline void	unlock() { return; }
+#else
+  explicit ThSafeOp() 
+    { 
+      int rc;
+      rc = pthread_mutex_init(&_mutx, NULL);
+      if (rc != 0) throw PException("ThSafeOp::ThSafeOp() / Error pthread_mutex_init");
+    }
+  ~ThSafeOp() 
+    {
+      int rc;
+      rc = pthread_mutex_destroy(&_mutx);
+      if (rc != 0) throw PException("ThSafeOp::~ThSafeOp() / Error pthread_mutex_destroy");
+    }
+  //! Locks the mutex object
+  inline void	lock()
+    { pthread_mutex_lock(&_mutx); } 
+  //! Unlocks the mutex object
+  inline void	unlock()
+    { pthread_mutex_unlock(&_mutx); }
+protected:
+  pthread_mutex_t _mutx;
+#endif
+};
+} // namespace SOPHYA
+
+#endif
