Is SynchronizationContext.Post () thread safe?
This is a pretty simple question and I think it is, but I cannot find a definitive answer. Is SynchronizationContext.Post()
threadsafe?
I have a member variable that contains the context of the main thread and _context.Post()
is called from multiple threads. I suppose it Post()
could be called simultaneously on the object. Should I do something like
lock (_contextLock) _context.Post(myDelegate, myEventArgs);
or is it not needed?
Edit:
MSDN states that "Any instance members are not guaranteed to be thread safe." Should I save lock()
then?
a source to share
Based on the MSDN documentation, then no, the method is SynchronizationContext.Post
not thread safe. Therefore, if there is no error in the documentation, you need to synchronize access to this method. I find it hard to believe that it is not thread safe on its own, but you cannot count on assumptions, especially when dealing with thread synchronization issues. This is not actually the case until Microsoft fixes the documentation and makes it thread-safe.
a source to share
SynchronizationContext.Post
is thread safe. The documentation did not take this fact into account.
I base this statement on Microsoft implementations AsyncOperation
and AsyncOperationManager
that assume they SynchronizationContext.Post
are thread safe (including any derived implementations).
a source to share