queue

An implemention of a Queue.

This Queue is thread safe (and block-free) to have separate threads reading and writing asynchronously. The Queue is NOT safe to have multiple threads reading or writing at the same time. i.e. A single thread can read while another single thread is writing. If you want to have mutiple threads reading or writing data from queue the threads will need to be sure to block themself.

To Create a queue

Queue!string queue; // Replace string with data type that values should be.

To add to queue

queue.put("This is a value that is added to queue!"); // Argument should be of oppropriate type.

To read data

foreach (string value; queue) {
// Deal with value here!
}

To read data safe to have multiple threads reading

synchronized {
foreach (string value; queue) {
// Deal with value here!
}
}

or

while (true) {
string value;
synchronized {
if (queue.empty) {
break;
}
value = queue.front;
queue.popFront;
}
// Deal with value here!
}

Members

Classes

Queue
class Queue(T)

/import core.sync.semaphore : Semaphore;

Examples

Queue!string queue; 

queue.add("A!");
queue.add("BBB!");
queue.add("CC!");

foreach (string value; queue) {
writeln(value);
}

queue.add("DDDDD!");
queue.add("EEEE!");

foreach (string value; queue) {
writeln(value);
}

Meta