Is there any threadsafe queue?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 13 09:49:46 UTC 2017


On Wednesday, September 13, 2017 07:51:19 John Burton via Digitalmars-d-
learn wrote:
> Is there any threadsafe queue in the standard library?
> I've not been able to find anything but thought I'd check before
> making my own.
>
> I want to be able to assemble messages (Which are just streams of
> bytes) in one thread into a struct and push them to the queue,
> and then have a another thread be able to read and process the
> messages. Single producer and consumer.

You could probably do what you want with std.concurrency, since most of what
it does is deal with sending and receiving data across threads, and that
should be queuing up messages as part of what it does. But if you're looking
for a data structure that you can mark as shared and have it handle all of
the locking for you so that it will work safely across threads, then no, the
standard library does not currently have anything like that. It's rather
lacking in containers in general at this point, let alone ones designed with
concurrency in mind. There may be something on code.dlang.org, but I don't
know. Regardless, I'd suggest that you first try and see if you can get
std.concurrency to work for your use case rather than jumping into
implementing any containers yourself.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list