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!stringqueue; // 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 (stringvalue; queue) {
// Deal with value here!
}
To read data safe to have multiple threads reading
synchronized {
foreach (stringvalue; queue) {
// Deal with value here!
}
}
or
while (true) {
stringvalue;
synchronized {
if (queue.empty) {
break;
}
value = queue.front;
queue.popFront;
}
// Deal with value here!
}
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
To add to queue
To read data
To read data safe to have multiple threads reading
or