Condition variables?

Sean Kelly sean at f4.ca
Tue Oct 2 09:18:09 PDT 2007


David Brown wrote:
> 
> I've normally seen condition code where the wait decision is made as part
> of the wait, not as part of the signal.  It allows different waiters to
> have different conditions (although having multiple waiters is complicated,
> and requires access to pthread_cond_broadcast).
> 
>    synchronized void add(T item)
>    {
>      add_item_to_queue;
>      count++;
>      ready.signal;
>    }
> 
>    synchronized T remove()
>    {
>      while (count == 0)
>        ready.wait
>      count--;
>      return remove_item_from_queue;
>    }

This is the classic approach for using condition variables, and I think 
it's more straightforward than the other method suggested.

> When I get to needing threads, I'll definitely look into your condition
> code, and see if I can get it to work.  It shouldn't be too complicated, I
> just wasn't sure how to get access to the object's monitor.
> 
> Is it possible to make 'this' synchronized?

Yup.  In Tango, it's possible to set a Mutex instance as the monitor for 
another object.  Doing so currently requires some pointer magic, but it 
would be simple to encapsulate in a function.

Sean



More information about the Digitalmars-d mailing list