# HG changeset patch
# User Alpar Juttner <alpar@cs.elte.hu>
# Date 1430991708 -7200
# Node ID 57a344e446c942d127d7004278af6c3ec50b4d43
# Parent  4c8ec448d59f342e4c1eda3b6b1f16379dd912b5# Parent  0900cfe4a84d4136dfabedd3175f822465c67faa
Merge bugfix #473 to branch 1.3

diff -r 4c8ec448d59f -r 57a344e446c9 lemon/cplex.cc
--- a/lemon/cplex.cc	Wed May 06 11:29:34 2015 +0200
+++ b/lemon/cplex.cc	Thu May 07 11:41:48 2015 +0200
@@ -37,37 +37,54 @@
     }
   }
 
+  void CplexEnv::incCnt()
+  {
+    _cnt_lock->lock();
+    ++(*_cnt);
+    _cnt_lock->unlock();
+  }
+
+  void CplexEnv::decCnt()
+  {
+    _cnt_lock->lock();
+    --(*_cnt);
+    if (*_cnt == 0) {
+      delete _cnt;
+      _cnt_lock->unlock();
+      delete _cnt_lock;
+      CPXcloseCPLEX(&_env);
+    }
+    else _cnt_lock->unlock();
+  }
+  
   CplexEnv::CplexEnv() {
     int status;
+    _env = CPXopenCPLEX(&status);
+    if (_env == 0)
+      throw LicenseError(status);
     _cnt = new int;
     (*_cnt) = 1;
-    _env = CPXopenCPLEX(&status);
-    if (_env == 0) {
-      delete _cnt;
-      _cnt = 0;
-      throw LicenseError(status);
-    }
+    _cnt_lock = new bits::Lock;
   }
 
   CplexEnv::CplexEnv(const CplexEnv& other) {
     _env = other._env;
     _cnt = other._cnt;
-    ++(*_cnt);
+    _cnt_lock = other._cnt_lock;
+    incCnt();
   }
 
   CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
+    decCnt();
     _env = other._env;
     _cnt = other._cnt;
-    ++(*_cnt);
+    _cnt_lock = other._cnt_lock;
+    incCnt();
     return *this;
   }
 
   CplexEnv::~CplexEnv() {
-    --(*_cnt);
-    if (*_cnt == 0) {
-      delete _cnt;
-      CPXcloseCPLEX(&_env);
-    }
+    decCnt();
   }
 
   CplexBase::CplexBase() : LpBase() {
diff -r 4c8ec448d59f -r 57a344e446c9 lemon/cplex.h
--- a/lemon/cplex.h	Wed May 06 11:29:34 2015 +0200
+++ b/lemon/cplex.h	Thu May 07 11:41:48 2015 +0200
@@ -23,6 +23,7 @@
 ///\brief Header of the LEMON-CPLEX lp solver interface.
 
 #include <lemon/lp_base.h>
+#include <lemon/bits/lock.h>
 
 struct cpxenv;
 struct cpxlp;
@@ -40,7 +41,11 @@
   private:
     cpxenv* _env;
     mutable int* _cnt;
+    mutable bits::Lock* _cnt_lock;
 
+    void incCnt();
+    void decCnt();
+    
   public:
 
     /// \brief This exception is thrown when the license check is not