www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Producer/Consumer Queue in D

reply Jeff <Jeff_member pathlink.com> writes:
I'm trying to learn how to do thread synchonization in D without waitable
objects like semaphores.

For example, how would you implement the classic multi-threaded
producer/consumer queue?  Where the consumer threads wait (zero cpu) until there
is an element inserted into the queue?

-Jeff
Jan 24 2006
parent Sean Kelly <sean f4.ca> writes:
Jeff wrote:
 I'm trying to learn how to do thread synchonization in D without waitable
 objects like semaphores.
 
 For example, how would you implement the classic multi-threaded
 producer/consumer queue?  Where the consumer threads wait (zero cpu) until
there
 is an element inserted into the queue?

On non-Windows systems, the classic approach is to use a condition variable, which is basically a fancy semaphore. On Windows systems, an event is typically used instead, though as events are somewhat broken this can be problematic if there are multiple competing consumers. But waiting isn't required in either case, as it's possible to test the variable or event without blocking. If you're aiming for truly lock-free code, look around for a lock-free slist. A low-level implementation is included with Visual Studio, and there are a number of open source versions available (though I don't have a link to one at the moment). Sean
Jan 24 2006