Threadsafe CplexEnv (#473)
authorAlpar Juttner <alpar@cs.elte.hu>
Wed, 06 May 2015 16:01:26 +0200
changeset 11390900cfe4a84d
parent 1137 ca4e4a5e9b6e
child 1140 f8ec64f78b5f
child 1147 138714057145
Threadsafe CplexEnv (#473)
lemon/cplex.cc
lemon/cplex.h
     1.1 --- a/lemon/cplex.cc	Wed May 06 11:28:38 2015 +0200
     1.2 +++ b/lemon/cplex.cc	Wed May 06 16:01:26 2015 +0200
     1.3 @@ -37,37 +37,54 @@
     1.4      }
     1.5    }
     1.6  
     1.7 +  void CplexEnv::incCnt()
     1.8 +  {
     1.9 +    _cnt_lock->lock();
    1.10 +    ++(*_cnt);
    1.11 +    _cnt_lock->unlock();
    1.12 +  }
    1.13 +
    1.14 +  void CplexEnv::decCnt()
    1.15 +  {
    1.16 +    _cnt_lock->lock();
    1.17 +    --(*_cnt);
    1.18 +    if (*_cnt == 0) {
    1.19 +      delete _cnt;
    1.20 +      _cnt_lock->unlock();
    1.21 +      delete _cnt_lock;
    1.22 +      CPXcloseCPLEX(&_env);
    1.23 +    }
    1.24 +    else _cnt_lock->unlock();
    1.25 +  }
    1.26 +  
    1.27    CplexEnv::CplexEnv() {
    1.28      int status;
    1.29 +    _env = CPXopenCPLEX(&status);
    1.30 +    if (_env == 0)
    1.31 +      throw LicenseError(status);
    1.32      _cnt = new int;
    1.33      (*_cnt) = 1;
    1.34 -    _env = CPXopenCPLEX(&status);
    1.35 -    if (_env == 0) {
    1.36 -      delete _cnt;
    1.37 -      _cnt = 0;
    1.38 -      throw LicenseError(status);
    1.39 -    }
    1.40 +    _cnt_lock = new bits::Lock;
    1.41    }
    1.42  
    1.43    CplexEnv::CplexEnv(const CplexEnv& other) {
    1.44      _env = other._env;
    1.45      _cnt = other._cnt;
    1.46 -    ++(*_cnt);
    1.47 +    _cnt_lock = other._cnt_lock;
    1.48 +    incCnt();
    1.49    }
    1.50  
    1.51    CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
    1.52 +    decCnt();
    1.53      _env = other._env;
    1.54      _cnt = other._cnt;
    1.55 -    ++(*_cnt);
    1.56 +    _cnt_lock = other._cnt_lock;
    1.57 +    incCnt();
    1.58      return *this;
    1.59    }
    1.60  
    1.61    CplexEnv::~CplexEnv() {
    1.62 -    --(*_cnt);
    1.63 -    if (*_cnt == 0) {
    1.64 -      delete _cnt;
    1.65 -      CPXcloseCPLEX(&_env);
    1.66 -    }
    1.67 +    decCnt();
    1.68    }
    1.69  
    1.70    CplexBase::CplexBase() : LpBase() {
     2.1 --- a/lemon/cplex.h	Wed May 06 11:28:38 2015 +0200
     2.2 +++ b/lemon/cplex.h	Wed May 06 16:01:26 2015 +0200
     2.3 @@ -23,6 +23,7 @@
     2.4  ///\brief Header of the LEMON-CPLEX lp solver interface.
     2.5  
     2.6  #include <lemon/lp_base.h>
     2.7 +#include <lemon/bits/lock.h>
     2.8  
     2.9  struct cpxenv;
    2.10  struct cpxlp;
    2.11 @@ -40,7 +41,11 @@
    2.12    private:
    2.13      cpxenv* _env;
    2.14      mutable int* _cnt;
    2.15 +    mutable bits::Lock* _cnt_lock;
    2.16  
    2.17 +    void incCnt();
    2.18 +    void decCnt();
    2.19 +    
    2.20    public:
    2.21  
    2.22      /// \brief This exception is thrown when the license check is not