Consider code that involves multiple threads writing to a shared data structure. Now each of these threads write objects to the shared container that are essentially unique. Take the following (fictitious) example:
class Logger
{
IDictionary<string, Log> logDictionary = new Dictionary<string, Log>();
// Method called by several other threads
void AddLog(Log log)
{
logDictionary[log.ID] = log;
}
// Method called by a separate thread
IList GetLog(/*some criteria*/)
{
// loop through logDictionary and
// create an IList based on criteria
}
}
I understand clearly that concurrent access to a shared object can be a problem if multiple threads try to update the same 'variable' (or slot) leading to race conditions. Now my question has 3 parts.
(1) Is synchronization necessary if the threads are making blind updates? (Even if they are updating the same slot/variable)?
(2) Is it thread-safe to assume, given that each thread is writing a unique object, there is no need to synchronize access to the shared dictionary/container?
(3) Does the call to GetLog() by another thread, really pose problems even if the dictionary is being updated simultaneously by other threads?