When I test the concurrent access to FreezeMap,I found it is much slower than single-thread method.My code like following:
1. When iThreadNum = 1, the g_GetNamesFromMap will use 600ms to finish per time. When iThreadNum = 30, it will takes 33s to finish. Is there anything wrong?Code:ObjectAdapterPtr g_pAdapter; vector<string> g_GetNamesFromMap(long beginTime,long endTime) { vector<string> back; Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db"); IHisDataMap hisDataMap(pConn, "map"); IHisDataMap::const_iterator pEnd = hisDataMap.upperBoundForTime(endTime); IHisDataMap::const_iterator pos; for(pos = hisDataMap.lowerBoundForTime(beginTime);pos!=pEnd;++pos) { back.push_back(pos->first); } return back; } vector<string> g_SetNamesToMap(string name,long time) { Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db"); IHisDataMap hisDataMap(pConn, "map"); IHisDataMap::iterator pos = hisDataMap.find(name); if(pos != hisDataMap.end()) { pos.set(time); }else { hisDataMap.insert(make_pair(name, time)); } } void JobThread::run() { m_bRunning = true; IceUtil::Time sleepTime = IceUtil::Time::milliSeconds(1000); while(true) { if(!m_bRunning) { break; }else { vector<string> names = g_GetNamesFromMap(0,ULONG_MAX); } ThreadControl::sleep(sleepTime); } } void main() { //Init communicator and g_pAdapter ............................. int iThreadNum = 30; for(int i=0;i<iThreadNum;++i) { JobThread pThread = new JobThread(); pThread->start(); } }
2. When some threads are invoking g_GetNamesFromMap, I will call g_SetNamesToMap to update the map. In this case, do I have to serialize the two function or just call g_SetNamesToMap directly?

Reply With Quote
