Re: semaphore question



David Schwartz wrote:

How would you use a mutex or semaphore to protect something other than
shared state of some kind? Shared state has nothing to do with the
particular code that accesses it, for example, the same protection is
required whether the state is accessed by the same code or different
code in multiple threads.

Ah, yes, now I get it:
Of course, you're right: Indeed it doesn't matter whether it is one piece of code (e.g. a piece of code that queues/dequeues elements into a linked list) or multiple pieces of code (e.g. one piece of code queues, the other dequeues) that would use a mutex.
And yes: you're right in stating that it's not the code per-se but the item that it's operating on that needs to be protected.

Two pieces of code modifying different data structures obviously can run in parallel, so a mutex should be used to prevent modification of a data item by whatever code wants to do that.

It is extremely bad practice and does in fact lead to misunderstandings
and bad code to think about synchronization mechanisms of any kind as
protecting code. Synchronization mechanisms protect the things that
more than one thread might want to access or modify, and that is not
code.

Concurrent access to code is perfectly safe so long as there isn't
conflicting access to the same data.

It is impossible to create a problem with conflicting code unless there
is also conflicting data.

If there's conflicting data, there will be a problem whether or not
there's concurrent use of the same code.

Yes, yes, yes, I see my mistake.

Just don't hit me, I'm wearing glasses ;-)

grumble ... musthave picked it up in some textbook ... grumble ...

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

.