Re: semaphore question
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 16 Aug 2006 13:04:41 +0200
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
.
- Follow-Ups:
- Re: semaphore question
- From: David Schwartz
- Re: semaphore question
- References:
- semaphore question
- From: Jack
- Re: semaphore question
- From: 42Bastian Schick
- Re: semaphore question
- From: Josef Moellers
- Re: semaphore question
- From: David Schwartz
- semaphore question
- Prev by Date: Re: semaphore question
- Next by Date: Re: semaphore question
- Previous by thread: Re: semaphore question
- Next by thread: Re: semaphore question
- Index(es):
Relevant Pages
|