lemon/cplex.cc
changeset 1148 2126945deb6a
parent 1092 dceba191c00d
child 1140 f8ec64f78b5f
equal deleted inserted replaced
12:7c4a874ab9e1 15:3171fa6a15d9
    35     if (!CPXgeterrorstring(0, status, _message)) {
    35     if (!CPXgeterrorstring(0, status, _message)) {
    36       std::strcpy(_message, "Cplex unknown error");
    36       std::strcpy(_message, "Cplex unknown error");
    37     }
    37     }
    38   }
    38   }
    39 
    39 
       
    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   
    40   CplexEnv::CplexEnv() {
    60   CplexEnv::CplexEnv() {
    41     int status;
    61     int status;
       
    62     _env = CPXopenCPLEX(&status);
       
    63     if (_env == 0)
       
    64       throw LicenseError(status);
    42     _cnt = new int;
    65     _cnt = new int;
    43     (*_cnt) = 1;
    66     (*_cnt) = 1;
    44     _env = CPXopenCPLEX(&status);
    67     _cnt_lock = new bits::Lock;
    45     if (_env == 0) {
       
    46       delete _cnt;
       
    47       _cnt = 0;
       
    48       throw LicenseError(status);
       
    49     }
       
    50   }
    68   }
    51 
    69 
    52   CplexEnv::CplexEnv(const CplexEnv& other) {
    70   CplexEnv::CplexEnv(const CplexEnv& other) {
    53     _env = other._env;
    71     _env = other._env;
    54     _cnt = other._cnt;
    72     _cnt = other._cnt;
    55     ++(*_cnt);
    73     _cnt_lock = other._cnt_lock;
       
    74     incCnt();
    56   }
    75   }
    57 
    76 
    58   CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
    77   CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
       
    78     decCnt();
    59     _env = other._env;
    79     _env = other._env;
    60     _cnt = other._cnt;
    80     _cnt = other._cnt;
    61     ++(*_cnt);
    81     _cnt_lock = other._cnt_lock;
       
    82     incCnt();
    62     return *this;
    83     return *this;
    63   }
    84   }
    64 
    85 
    65   CplexEnv::~CplexEnv() {
    86   CplexEnv::~CplexEnv() {
    66     --(*_cnt);
    87     decCnt();
    67     if (*_cnt == 0) {
       
    68       delete _cnt;
       
    69       CPXcloseCPLEX(&_env);
       
    70     }
       
    71   }
    88   }
    72 
    89 
    73   CplexBase::CplexBase() : LpBase() {
    90   CplexBase::CplexBase() : LpBase() {
    74     int status;
    91     int status;
    75     _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
    92     _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");