Re: semaphore question



David Schwartz wrote:
Josef Moellers wrote:


You protect neither files nor global data.


Mutexes protect data.

Yes and no.

Mutexes serialize execution of critical sections.
What you do inside these critical sections is up to you.
You might modify global data, you might modify the internal state of a device in multiple steps, ...

You serialize concurrent execution of code.


No, you serialize concurrent accesses to the same data.


You can even protect serial (or other) communications using mutexs!


I think this is a very bad way to think about mutexes. Mutexes are
associated with the objects they protect, not the code that accesses
them. Code should not acquire a mutex that protects that code but a
mutex that protects the object that code manipulates.

That is a specific mutex.
Generally speaking (and that was what I was referring to), a mutex is a semaphore initialized with 1.

Thinking about mutexes as protecting code leads to people putting
mutexes around code that has no need to be protected. Only shared data
(or resources of some kind) requires protection.

Any global state that might be modified concurrently requires protection.

Code never needs to be protected. Concurrent accesses of the same code
is totally safe and permitted.

It depends on what the code does.

But I guess I'm thinking of a generic mutex (1-Semaphore) and you're thinking of a specific use of a mutex.

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

.



Relevant Pages

  • Re: Why does Microsoft never document threading right?
    ... "A critical section is a lightweight mechanism for allowing only one ... Unlike events, mutexes, and semaphores, which are also used ... It is *implemented* using a mutex, but it is a critical section.... ... have their own 'CRITICAL_SECTION' object to protect the data in the ...
    (comp.programming.threads)
  • Re: Common Lisp / Scheme comparison
    ... > mutexes are procedures that themselves have to protect and unprotect ... do you get a lock on the code that releases ... It is best not to make your locking code too complex. ...
    (comp.lang.lisp)
  • Re: Simple Flag on SMP
    ... >>mutexes or semaphores to protect your data. ... > Strange that I haven't noticed anyone yet mentioning functions ...
    (comp.os.linux.development.system)
  • Re: temporary disable thread switching using pthreads
    ... without the need to protect the access to common ... objects by mutexes everywhere in the code. ... Use locks to protect the ... How would disabling thread switching help if other threads are ...
    (comp.programming.threads)
  • Re: WaitForSingleObject() will not deadlock
    ... And in what way am I wrong about Windows mutexes? ... expectations would violate the *documented* mutex behavior). ... building only experimental systems (my first synchronization system was ...
    (microsoft.public.vc.mfc)