0

I read on SO in an answer of a question: "A lock allows only one thread to enter the part that's locked" and many people have voted that up. The answer further scales up the definition to explain mutex and semaphore.

In another place (on SO as well as on many other sites) I saw piece of code that uses "critical section" to create read lock.

I believe they both are right. But I wonder if despite of being said in theory "read lock allows concurrent readers", in actual implementation it's still just single thread allowed at a time?

Community
  • 1
  • 1
Atul
  • 3,778
  • 5
  • 47
  • 87
  • Which language? What implementation? Looks like your example is MS/Windows CriticalSection? Note: A Semaphore allows N locks on a resource instead of just 1. (So, for example 10 simultaneous readers could be allowed of a resource) If you mean, is a low-level single lock used to increment the internal counters of Semaphore for very brief periods during state change, the answer would be "sometimes". –  Sep 05 '13 at 19:53
  • Well I am not talking about any specific language or particular implementation as such. But just concept wise I can see contradictions between "A lock allows only one thread to enter" and "Read lock allows concurrent readers" – Atul Sep 06 '13 at 05:22
  • Could it be they are talking about different kinds of locks? (lock, Mutex and Semaphore are quite different. Didn't the first answer of your first link sum it up?) CriticalSection is a somewhat low-level component of windows that allows process-specific thread exclusion. It's a different animal, why would it necessarily behave according to lock/Mutex/Semaphore? Further, the code example you site is just using lock to guard an integer count of readers making the "sometimes" true in my previous statement. Personally, I'd keep a list of known readers (and time them out) count is corruptible –  Sep 06 '13 at 11:53

0 Answers0