Take the following code as an example, simplified so ignore the fact the data is null:
ConcurrentBag<object> _mySharedData;
bool _stop = false;
public void SetThreads()
{
foreach(var item in _mySharedData)
{
Task.Factory.StartNew((state) => {
while (!_stop)
DoStuffWithItemAsReference(item);
}, TaskCreationOptions.LongRunning);
}
}
The shared data holds a collection of settings, each one corresponding to what is running in that thread. The issue however, is that sometimes they need to look at the settings for the other threads too. Is the code thread safe as long as I add a lock? If settings are updated for a thread from within another thread, how does that work? Because now we have a thread that has a reference to an item in the _mySharedData collection, is it safe to alter its properties?
Thanks