Inserting into a map corrupting the map data
Hi All,
I am having a map of <long, long>. See the definition of the map
typedef map<long, long> CIndexToDataMap;
CIndexToDataMap m_IndexToDataMap;
This map is member of a COM component(dll) running in 64bit platform.
This map is shared between two threads(Thread1Method and
Thread2Method) and protected by Critical Section. Thread1Method
inserting the data and Thread2Method accessing the data based on the
index.
When Thread1Method inserting the data then data corruption is
happening. Before inset i dump the size and all the values of the map
and everthing is correct. But after inserting size is correct (suppose
11) but data showing only few (2) records. Find the Thread1Method()
and Thread2Method implementation
HRESULT Thread1Method()
{
{
AutoLock autoLock(m_CritSec);
Trace(_T("Before inserting Map size is = %d...\n"),
m_IndexToDataMap.size()); // size 10
CIndexToDataMap ::iterator itr = m_IndexToDataMap.begin();
while (itr != m_IndexToDataMap.end())
{
Trace(_T("Index%d corresponds to data %d."), itr->first, itr-
second); // can see all the 10 data
itr++;
}
}
{
AutoLock autoLock(m_CritSec);
m_IndexToDataMap.insert(pair<long, long>(nIndex, ldata));
Trace(_T(" After inserting Map size is = %d...\n"),
m_IndexToDataMap.size()); //size is 11
for(CIndexToDataMap ::iterator itr1 =
m_IndexToDataMap.begin(); itr1 != m_IndexToDataMap.end(); ++itr1)
{
Trace(_T("Index%d corresponds to data %d."), itr1->first, itr1-
second); //PROBLEM HERE, can see only 2 records
}
}
HRESULT Thread2Method(nIndex)
{
AutoLock autoLock(m_CritSec);
if (m_IndexToDataMap.find(nIndex) != m_IndexToDataMap.end())
{
nScanConfigIndex = m_IndexToDataMap[nIndex];
return TRUE;
}
}
Please suggest whats wrong. Thanks in advance
Aditya