【发布时间】:2014-07-30 00:59:12
【问题描述】:
我有一张这样定义的地图。
typedef std::map< tstring, unsigned int > ErrorToCount_T;
ErrorToCount_T m_ErrorToSuppress;
我就是这样用的。
ErrorToCount_T::iterator itr = m_ErrorToSuppress.find( val );
if( itr != m_ErrorToSuppress.end())
{
if( (itr->second) % m_LogFreq == 0)
//Do something
else
//Do something else
InterlockedIncrement( &itr->second);
}
我看到了this,我知道 find 是线程安全的。但我在想 InterlockedIncrement( &itr->second) 也将是线程安全的?上面的代码线程安全吗? 在多线程环境中,此映射中绝对没有插入。
【问题讨论】:
-
不,在一个线程中读取变量并在另一个线程中修改它绝不是线程安全的。你需要一个真正的锁,就像一个互斥锁。
-
我希望你的意思是绝对没有删除和插入..
标签: multithreading map stl thread-safety interlocked-increment