Reputation: 20139
Suppose I have a data structure S in memory. S is complex, say a tree or a graph. I have two threads travelling through S, and they may hit and try to read the same address at the same time. My question is: is there any kind of implicit locking at this point such that contention between the threads arises? If it is of value, I'm using C, pthreads on a regular SMP machine.
Upvotes: 3
Views: 91
Reputation: 7203
First, no implicit locking is done in C. C is very much a "here's the rope" language in that you are always free to hang yourself.
Also, please note that reading without locking is only safe when you are never writing. If you ever change the values, than reading can get you the wrong value.
Last, two cool tools for helping you decide if you need to lock or not when you are unsure are DRD and helgrind (valgrind tools). Both of these are threading tools that can detect various kinds of thread-related errors. Just know that they will err on the side of sensitivity.
Upvotes: 0
Reputation: 76908
No. There is no need for locking with read-only operations.
There would only be locking if you explicitly implement it. This would usually be done when there's a chance of another thread modifying (writing) the structure at the same time.
Upvotes: 3