Harry Van Haaren wrote:
That's the background to this question: Is mutex
locking the only way to
ensure that variables are only
being accessed by one thread at a time? Is it the "right" way to go
about it?
No, there are also semaphores and read-write locks. RW-locks are
especially useful in cases where there's a single writer but multiple
readers (typically any data distribution).
In general, threading is a wrong way to go if you find sharing and
locking a lot of data between the threads. Threads should be able to
execute fairly independently to properly utilize resources. Naturally
there are typically scatter-gather points which are supposed to be short
compared to overall thread execution time.