我正在多线程环境中开发数据聚合对象,并且遇到以下情况:
int counts;
// This event is triggered by many threads at the same time
public void OnDataReceived(DataEvent evt)
{
counts += evt.counts;
}
我的问题是这样做是否安全
counts += evt.counts;
或者我需要类似的东西
lock(lockObject)
{
counts += evt.counts;
}