
Rust: learning how to share a queue between threads - dbeck74
http://dbeck.github.io/Learning-Rust-Sharing-My-Queue-Between-Threads/
======
yarper
[http://rustbyexample.com/std_misc/channels.html](http://rustbyexample.com/std_misc/channels.html)

This is how you share a queue between threads, the title is super-clickbait.
It doesn't seem a surprise that stdlib doesn't have a circular buffer, I have
no idea what it'd be used for?

~~~
mmerickel
I believe a channel can only have one receiver and one sender, no? So you
can't share the channel between multiple workers.

~~~
yarper
This is the case for spsc (single producer, single consumer) channels, but the
example I posted is for a mpsc (multiple producer single consumer) channel.
Any number of threads can push data into it, as the example shows!

Having multiple receivers is slightly more complex, and you can't use
std::sync::mpsc (obviously) but they are also possible.

------
viperscape
If you're curious check out oyashio:
[https://github.com/viperscape/oyashio](https://github.com/viperscape/oyashio)
It's a thread safe spmc in Rust that uses promises underneath. If I were to
rewrite it, I'd use a Vec with promises and a cursor of sorts, instead of the
of linked list I came up with.

~~~
yarper
You could also use this [https://doc.rust-
lang.org/std/sync/struct.Condvar.html](https://doc.rust-
lang.org/std/sync/struct.Condvar.html) (all block, notify one) to implement a
"work stealing" style multiple consumers channel that'd be pretty cool. I
think various incantations of this have been in and out of Rust's stdlib (I
couldn't tell you why though?).

