Re: semaphore question
- From: "David Schwartz" <davids@xxxxxxxxxxxxx>
- Date: 16 Aug 2006 01:36:38 -0700
Josef Moellers wrote:
You protect neither files nor global data.
Mutexes protect data.
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.
For example, suppose you have a function to print the contents of a
node. It should acquire the mutex that protects that node, whatever
node it happens to be printing. It is nonsense to say there should be
some mutex associated with the node printing code.
If I have a setup where I have an object of a particular type, and have
30 of those objects and 10 pieces of code that access them, should I
have 30 mutexes or 10?
Suppose you have some code that can manipulate either a foo or a bar.
Some other code manipulates foos and acquires the foo mutex. Some other
code manipulates bars and acquires the bar mutex. The code that
manipulates either a foo or a bar should do what? It should acquire the
foo mutex when it manipulates a foo and the bar mutex when it
manipulates the bar, even if it uses exactly the same code for both
manipulations.
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.
Code never needs to be protected. Concurrent accesses of the same code
is totally safe and permitted.
DS
.
- Follow-Ups:
- Re: semaphore question
- From: Jack
- Re: semaphore question
- From: Josef Moellers
- Re: semaphore question
- References:
- semaphore question
- From: Jack
- Re: semaphore question
- From: 42Bastian Schick
- Re: semaphore question
- From: Josef Moellers
- semaphore question
- Prev by Date: Re: Inject code where in device driver for checking on receive packets?
- Next by Date: Re: semaphore question
- Previous by thread: Re: semaphore question
- Next by thread: Re: semaphore question
- Index(es):
Relevant Pages
|