Boost logo

Boost :

From: Beman Dawes (bdawes_at_[hidden])
Date: 2003-07-06 20:05:36


At 09:22 AM 6/4/2003, Stefan Seefeld wrote:
>hi there,
>
>I'v been trying to find some info as to why semaphores
>are considered harmful by the boost::thread authors,
>without luck. Is there any concise text describing
>the problem ?

I've expanded the FAQ entry to read:

Why has class semaphore disappeared?

Semaphore was removed as too error prone. The same effect can be achieved
with greater safety by the combination of a mutex and a condition variable.
Dijkstra (the semaphore's inventor), Hoare, and Brinch Hansen all
depreciated semaphores and advocated more structured alternatives.
[Andrews-83] summarizes typical errors as "omitting a P or a V, or
accidentally coding a P on one semaphore and a V on on another", forgetting
to include all references to shared objects in critical sections, and
confusion caused by using the same primitive for "both condition
synchronization and mutual exclusion".

The [Andrews-83] reference is to Gregory R. Andrews, Fred B. Schneider,
Concepts and Notations for Concurrent Programming, ACM Computing Surveys,
Vol. 15, No. 1, March, 1983.
http://www.acm.org/pubs/citations/journals/surveys/1983-15-1/p3-andrews/

>I'v been using semaphores for years and can't think of
>what should be wrong with it.

That's what most programmers said about goto when Dijkstra's "Go To
Statement Considered Harmful" was published. If you go back and read his
letter (http://www.acm.org/classics/oct95/), you could substitute
"semaphore" for "go to statement" in the key sentence: "The go to statement
as it stands is just too primitive; it is too much an invitation to make a
mess of one's program."

Goto's work (or worked; how long since you've seen one in a program?)
Semaphores work. But we are better off without both of them.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk