Boost logo

Boost :

From: Jon Biggar (jon_at_[hidden])
Date: 2003-07-07 00:33:20


Beman Dawes wrote:
> 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.

There is actually one case that needs a semaphore that has no reasonable
alternative in pthreads. The only pthread synchronization operation
that is asynch-reentrant safe (i.e. can be called from a signal handler)
is signaling a pthread semaphore.

It would be nice if there was some abstraction available in boost
threads that used this mechanism internally to get the needed behavior,
but encapsulated to hide the error-proneness of a raw semaphore.

-- 
Jon Biggar
Floorboard Software
jon_at_[hidden]
jon_at_[hidden]

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