From 21898660f989df3ba0b7707a9a1d25b180e1488d Mon Sep 17 00:00:00 2001 From: Scott R Charlton Date: Fri, 10 May 2013 22:15:00 +0000 Subject: [PATCH] new threading mutex patch from tmishima@jcity.maeda.co.jp git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/trunk@7705 1feff8c3-07ed-0310-ac33-dd36852eb9cd --- src/IPhreeqc.cpp | 9 ++++++++- src/IPhreeqc.hpp | 2 +- src/IPhreeqcLib.cpp | 6 +----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/IPhreeqc.cpp b/src/IPhreeqc.cpp index e330500a..e85fb207 100644 --- a/src/IPhreeqc.cpp +++ b/src/IPhreeqc.cpp @@ -3,6 +3,8 @@ #include "IPhreeqc.hpp" // IPhreeqc #include "Phreeqc.h" // Phreeqc +#define _INC_PHREEQC_H +#include "thread.h" #include "Debug.h" // ASSERT #include "ErrorReporter.hxx" // CErrorReporter @@ -41,7 +43,7 @@ IPhreeqc::IPhreeqc(void) , PhreeqcPtr(0) , input_file(0) , database_file(0) -, Index(IPhreeqc::InstancesIndex++) +/* , Index(IPhreeqc::InstancesIndex++) */ { char buffer[80]; @@ -53,8 +55,11 @@ IPhreeqc::IPhreeqc(void) ASSERT(this->PhreeqcPtr->phast == 0); this->UnLoadDatabase(); + mutex_lock(&map_lock); + this->Index = IPhreeqc::InstancesIndex++; std::map::value_type instance(this->Index, this); std::pair::iterator, bool> pr = IPhreeqc::Instances.insert(instance); + mutex_unlock(&map_lock); ::sprintf(buffer, PUNCH_FILENAME_FORMAT, this->Index); this->SelectedOutputFileName = buffer; @@ -83,11 +88,13 @@ IPhreeqc::~IPhreeqc(void) delete this->WarningReporter; delete this->ErrorReporter; + mutex_lock(&map_lock); std::map::iterator it = IPhreeqc::Instances.find(this->Index); if (it != IPhreeqc::Instances.end()) { IPhreeqc::Instances.erase(it); } + mutex_unlock(&map_lock); } VRESULT IPhreeqc::AccumulateLine(const char *line) diff --git a/src/IPhreeqc.hpp b/src/IPhreeqc.hpp index 0cb8bf15..c1dcdd7c 100644 --- a/src/IPhreeqc.hpp +++ b/src/IPhreeqc.hpp @@ -885,7 +885,7 @@ protected: friend class IPhreeqcLib; static std::map Instances; static size_t InstancesIndex; - const size_t Index; + size_t Index; #if defined(_MSC_VER) /* reset warning C4251 */ diff --git a/src/IPhreeqcLib.cpp b/src/IPhreeqcLib.cpp index 46980933..866425a7 100644 --- a/src/IPhreeqcLib.cpp +++ b/src/IPhreeqcLib.cpp @@ -904,9 +904,7 @@ IPhreeqcLib::CreateIPhreeqc(void) IPhreeqc* IPhreeqcPtr; try { - mutex_lock(&map_lock); IPhreeqcPtr = new IPhreeqc; - mutex_unlock(&map_lock); n = (int) IPhreeqcPtr->Index; } catch(...) @@ -924,9 +922,7 @@ IPhreeqcLib::DestroyIPhreeqc(int id) { if (IPhreeqc *ptr = IPhreeqcLib::GetInstance(id)) { - mutex_lock(&map_lock); delete ptr; - mutex_unlock(&map_lock); retval = IPQ_OK; } } @@ -961,4 +957,4 @@ IPhreeqcLib::GetInstance(int id) // { // delete ip_list[i]; // } -//} \ No newline at end of file +//}