Re: Glib Semaphores

> A possible advantage of semaphores is that because the shared data they protect
> is a simple counter, atomic operations may be exploited to perform the
> increment and decrement operations. For that, semaphores have to be implemented
> specially, rather than in terms of other primitives such as mutexes and
> conditions.  This advantage disappears when the semaphores are used in
> conjunction with data that must be separately protected anyway (such as
> consumer/producer queue), for then the counter might as well be made part of
> the queue structure and protected by the queue's mutex, e.g.

Probably true, but then some things are better represented one way 
while others are better represented in others.  I assume the user
will chose the most appropriate representation.

A typical example of producer/consumer expressed if lock step operation
is necessary can be cleanly written with semaphores like this. 

   // spawn threads
   process.down();  // wait until all processes die to terminate

   while (want_items)
       // eat up an item
       // do some more processing which is not shared

   while (can_produce)
       // produce some items.
       // more unshared processing

Here, I don't need any more mutexs nor do I need to worry about
the recombining.  The semaphore process represents threads completed
so arguments about representing some queue length don't really 
> To prove that the queue is not
> misused, for example by trying to remove an item from the empty queue, one has
> to prove that the semaphores have reasonable values at all times.  In a trivial
> example, this doesn't pose a problem, but in large, complex multithreaded
> software, this kind of uncertainty can cause real headaches.

This simply means that semaphores weren't the proper way to express that
large, complex multithreaded software.  They do have their uses especially 
for tasks where you spawn some threads let then work and then recombine.
I certain don't think adding a semaphore concept is likely to force it
on the users.  

You seem to be implying that since you can do it one way, there is 
no need for a simpler to use concept for quick and dirty threading tasks.  
I think that it certain is safe for some tasks and some programmer will
find it of use.   Since the cost of adding it is fairly insignificant, I 
don't see the fact that is not useful in very complex programs as being 
a reason for not adding such a common concept.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]