Re: semaphore question




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

.



Relevant Pages

  • Re: multiple cores, multiple threads?
    ... mutexes for chunks of array locations. ... You can minimize your locking operations by creating a data structure ... a data structure that holds results, also protected by a mutex. ... Acquire the mutex that protects the collection or work that needs ...
    (comp.programming.threads)
  • Re: data protected not by mutexes but program logic
    ... Since the mutex doesn't know what it protects, ... it can know that it need not pay attention to memory sync operations ... other threads' coherency actions associated with it, ...
    (comp.programming.threads)
  • Re: Linux pthread_cond_broadcast waking only one thread out of four waiting
    ... Unless they all hold the same mutex, there is no way they can make ... If there's no 'the mutex that protects the ... The condition variable mechanism is very light. ... the algorithm run slower because of either false sharing, ...
    (comp.programming.threads)
  • Re: Pthread programming: about the lost condition signals.
    ... Furthermore, for sure, it is against structured programming. ... known safe, so long as it is done while the mutex is held. ... level mutex protects the shared resource only. ...
    (comp.programming.threads)
  • Re: Waiting for mutiple objects
    ... I need to wait for an array of mutexes or any other sync ... protected by a single mutex which also protects a ...
    (comp.unix.programmer)