COIN-OR::LEMON - Graph Library

Ticket #473: 0900cfe4a84d.patch

File 0900cfe4a84d.patch, 2.1 KB (added by Alpar Juttner, 10 years ago)
  • lemon/cplex.cc

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1430920886 -7200
    #      Wed May 06 16:01:26 2015 +0200
    # Node ID 0900cfe4a84d4136dfabedd3175f822465c67faa
    # Parent  ca4e4a5e9b6e09eb28ea7e6b54bc44bdd8ef1bda
    Threadsafe CplexEnv (#473)
    
    diff --git a/lemon/cplex.cc b/lemon/cplex.cc
    a b  
    3737    }
    3838  }
    3939
     40  void CplexEnv::incCnt()
     41  {
     42    _cnt_lock->lock();
     43    ++(*_cnt);
     44    _cnt_lock->unlock();
     45  }
     46
     47  void CplexEnv::decCnt()
     48  {
     49    _cnt_lock->lock();
     50    --(*_cnt);
     51    if (*_cnt == 0) {
     52      delete _cnt;
     53      _cnt_lock->unlock();
     54      delete _cnt_lock;
     55      CPXcloseCPLEX(&_env);
     56    }
     57    else _cnt_lock->unlock();
     58  }
     59 
    4060  CplexEnv::CplexEnv() {
    4161    int status;
     62    _env = CPXopenCPLEX(&status);
     63    if (_env == 0)
     64      throw LicenseError(status);
    4265    _cnt = new int;
    4366    (*_cnt) = 1;
    44     _env = CPXopenCPLEX(&status);
    45     if (_env == 0) {
    46       delete _cnt;
    47       _cnt = 0;
    48       throw LicenseError(status);
    49     }
     67    _cnt_lock = new bits::Lock;
    5068  }
    5169
    5270  CplexEnv::CplexEnv(const CplexEnv& other) {
    5371    _env = other._env;
    5472    _cnt = other._cnt;
    55     ++(*_cnt);
     73    _cnt_lock = other._cnt_lock;
     74    incCnt();
    5675  }
    5776
    5877  CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
     78    decCnt();
    5979    _env = other._env;
    6080    _cnt = other._cnt;
    61     ++(*_cnt);
     81    _cnt_lock = other._cnt_lock;
     82    incCnt();
    6283    return *this;
    6384  }
    6485
    6586  CplexEnv::~CplexEnv() {
    66     --(*_cnt);
    67     if (*_cnt == 0) {
    68       delete _cnt;
    69       CPXcloseCPLEX(&_env);
    70     }
     87    decCnt();
    7188  }
    7289
    7390  CplexBase::CplexBase() : LpBase() {
  • lemon/cplex.h

    diff --git a/lemon/cplex.h b/lemon/cplex.h
    a b  
    2323///\brief Header of the LEMON-CPLEX lp solver interface.
    2424
    2525#include <lemon/lp_base.h>
     26#include <lemon/bits/lock.h>
    2627
    2728struct cpxenv;
    2829struct cpxlp;
     
    4041  private:
    4142    cpxenv* _env;
    4243    mutable int* _cnt;
     44    mutable bits::Lock* _cnt_lock;
    4345
     46    void incCnt();
     47    void decCnt();
     48   
    4449  public:
    4550
    4651    /// \brief This exception is thrown when the license check is not