Suppose I have a byte array, Private Data as Byte()
. This array is private within a class. The class provides public functions for reading and writing to Data
.
This class can be accessed by multiple threads, so I want to avoid a situation where reading from it and writing from it don't happen at the same time.
For now, I am using SyncLock to avoid issues. Can I put SyncLock Data
in just the write functions, or does it need to be in the read functions? Or, both?
I don't have a specific code example in mind. I am just curious if there is any benefit to locking for both read and write functions if the writing functions' SyncLock will make writing have exclusive access to it in the first place.
SyncLock
when writing to the byte array, I shouldn't have this problem, should I? – Lala