Index: trunk/SophyaLib/BaseTools/segdatablock.h
===================================================================
--- trunk/SophyaLib/BaseTools/segdatablock.h	(revision 3213)
+++ trunk/SophyaLib/BaseTools/segdatablock.h	(revision 3214)
@@ -69,5 +69,4 @@
   {
     mSRef = NULL;
-    if (gThsop == NULL) gThsop = new ThSafeOp;
     SetSize(segsz, nbseg);
   }
@@ -75,8 +74,8 @@
   SegDataBlock(const SegDataBlock<T>& a)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
   //! copy constructor with specification of flags for data sharing and element value copy
@@ -84,8 +83,8 @@
   {
     if (share) {
-      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+      a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
       mSRef = a.mSRef;
       mSRef->nref++;
-      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+      a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     }
     else {
@@ -97,11 +96,10 @@
   SegDataBlock(const SegDBInterface<T>& a)
   {
-    if (gThsop == NULL) gThsop = new ThSafeOp;
     SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a);
     if (sdb != NULL) { 
-      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+      sdb->mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
       mSRef = sdb->mSRef;
       mSRef->nref++;
-      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+      sdb->mSRefg->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     }
     else Clone(a, true);
@@ -111,7 +109,5 @@
   {
     //DEL    cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl;
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
   }
 
@@ -121,8 +117,9 @@
   {
     T * p = new T[mSRef->segsize];
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->dseg.push_back(p);
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
-    return( mSRef->dseg.size());
+    size_t rs = mSRef->dseg.size();
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    return( rs );
   }
 
@@ -131,21 +128,21 @@
   virtual void SetSize(size_t segsz, size_t nbseg=0)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
     mSRef = new SDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = segsz;
     mSRef->dsid = AnyDataObj::getUniqueId();
     for(size_t k=0; k<nbseg; k++) Extend_P();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
 //! Shares the data between two SegDataBlock objects
   void Share(const SegDataBlock<T>& a)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
 
@@ -153,10 +150,10 @@
   void Clone(const SegDBInterface<T> & a, bool cpval=true)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
     mSRef = new SDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = a.SegmentSize();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     for(size_t k=0; k<a.NbSegments(); k++) {
       Extend();
@@ -254,6 +251,11 @@
   {
     if (mSRef == NULL) return;
+    mSRef->gThsop.lock();
     mSRef->nref--;
-    if (mSRef->nref > 0)  { mSRef = NULL; return; }
+    if (mSRef->nref > 0)  { 
+     mSRef->gThsop.unlock();
+     mSRef = NULL; 
+      return; 
+    }
     //DEL cout << " DEBUG-SegDataBlock::Delete() NbSegments() = " << NbSegments() << endl;
     for(size_t k=0; k<NbSegments(); k++) {
@@ -261,4 +263,5 @@
       mSRef->dseg[k] = NULL;
     }
+    mSRef->gThsop.unlock();
     delete mSRef;
     mSRef = NULL;
@@ -280,12 +283,11 @@
     std::vector<T *> dseg;
     size_t segsize;
+    ThSafeOp gThsop;  // Mutex for thread safe operation 
   } SDREF;
   /*! \endcond */
   SDREF *  mSRef;    //!< SDREF structure for reference sharing
 
-  static ThSafeOp* gThsop;  //!< Mutex for thread safe operation 
 };
 
-template <class T> ThSafeOp* SegDataBlock<T>::gThsop  = NULL;  // static member initialized to NULL
 
 //! Definition of operator \<\< for ascii formatted output of SegDataBlock
