# Any issues with multiple threads writing to a single queue which is consumed by another thread

**URL:** https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846
**Category:** New to Julia
**Created:** [November 20, 2021, 9:55pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846 "2021-11-20T21:55:26Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 20, 2021, 9:55pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/1 "2021-11-20T21:55:26Z")

</div>

got dual xeon 2699 v3 so 36 cores. If I run threaded code each of which generates a stream of data values, from say a temperature sensor, on 10 of them and in the threaded code each writes it’s thread number and a data value to a single queue which is consumed by another thread on another core, are there any issues?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 20, 2021, 10:57pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/2 "2021-11-20T22:57:09Z")

</div>

Depends on what queue is. If queue is [`Channel`](https://docs.julialang.org/en/v1/base/parallel/#Base.Channel), for example, it should work fine.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 20, 2021, 11:09pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/3 "2021-11-20T23:09:00Z")

</div>

sorry I thought it was clear. I mean the queue in

[https://juliacollections.github.io/DataStructures.jl/latest/stack\_and\_queue/](https://juliacollections.github.io/DataStructures.jl/latest/stack_and_queue/)

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [November 20, 2021, 11:13pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/4 "2021-11-20T23:13:55Z")

</div>

I would be surprised if that worked without races. AFAIK multi threading is not supported in DataStructures.jl and the data structures are not lock free. You would have to implement your own locking scheme.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 20, 2021, 11:37pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/5 "2021-11-20T23:37:40Z")

</div>

really how is that possible? I would have thought that the act of using a queue negates any possibility of a race condition. The thread just puts to the next slot in the queue. Remember the queue is running on different thread.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 21, 2021, 12:14am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/6 "2021-11-21T00:14:24Z")

</div>

How do you send the info between threads? If the queue is private to a single thread that manages it, then any race problem will not happen at the queue, but instead they will depend on how this queue-manager thread gets the information from the other threads.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 21, 2021, 12:27am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/7 "2021-11-21T00:27:14Z")

</div>

the queue will be opened by the threads

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 21, 2021, 12:31am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/8 "2021-11-21T00:31:47Z")

</div>

You are really not giving any sufficient amount of detail for us to give you an answer.

If you meant that every thread will have shared access to the queue then yes, race conditions can happen. One thread may be interrupted in the middle of adding a new item, and when the execution returns to the thread it can overwrite the position that was unused before.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 21, 2021, 1:21am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/9 "2021-11-21T01:21:37Z")

</div>

I am so sorry @Henrique_Becker you are completely right I haven’t been too clear on this at all. I DID mean each thread had access to the same queue and thought that there would be NO race condition. I suppose it’s back to the drawing board. My concept was that I would allocate a process to a thread which would only gather data and place it on a shared queue running on a separate thread. I thought that basic queue functionality would kick in that the “data” thread could NOT be interrupted by anything (thread isolation) so the push to the queue would always succeed. So I would start up three threads, two would gather data. The third thread would run the queue. The two data threads would open the queue and write to it. That was how I saw this working. I used to do the same thing on a HP system using a shared databus called $receive and micromessages.

Thank you all for your help

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [November 21, 2021, 1:38am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/10 "2021-11-21T01:38:32Z")

</div>

As others have mentioned, `Channel` is the go-to solution if you need a concurrent queue; i.e., a thread-safe FIFO container. Also, note that you need to assume that the data structure APIs are not thread-safe unless it is explicitly documented as such. If you really want to use non-thread-safe data structure like the queue in DataStructures.jl, put a lock (`ReentrantLock`) around it. However, you won’t get extra functionality like waitable `take!` implemented by `Channel`. If you are sure that `Channel` won’t work for you, have a look at various concurrent queue implementations in [https://github.com/JuliaConcurrent/ConcurrentCollections.jl](https://github.com/JuliaConcurrent/ConcurrentCollections.jl) (note that this package is pretty new, not tested well in the wild, and requires Julia 1.7)

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 21, 2021, 1:45am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/11 "2021-11-21T01:45:23Z")

</div>

> [@anon69491625](#):
>
> The two data threads would open the queue and write to it.

as @tkf said, I’m pretty sure what you want is a Channel. It’s designed to do exactly what you want. Your “producer” threads write to it, and a consumer thread reads from it. You may want to give it a buffer of length approximately equal to the number of producer threads, or the number of threads x2 for example. This means the producers will not block (provided the consumer consumes at an appropriate pace).

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 21, 2021, 1:49am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/12 "2021-11-21T01:49:58Z")

</div>

@dlakelan @tkf @Henrique_Becker@fredrikekre Thank you all for taking the time to set me straight. I misread the documentation for DataStructures.jl . I will spend some time researching Channel. Thank you all for taking the time on a Saturday to help a newbie who clearly can’t read 🙂

---

<div class="post-metadata">

### Author: ![nielsls](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nielsls/32/18482_2.png) [@nielsls](https://discourse.julialang.org/u/nielsls)
#### Post date: [November 21, 2021, 8:08am UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/13 "2021-11-21T08:08:32Z")

</div>

Can we really be certain that channels are threadsafe? The docs do not seem to state that anywhere. Furthermore, in the docs channels are only featured in relation to asynchronous programming and not in true multithreading.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 21, 2021, 1:12pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/14 "2021-11-21T13:12:15Z")

</div>

@nielsls it seems they are but I’ll find out 🙂 thank you for taking an interest.

[channels are threadsafe](https://stackoverflow.com/questions/65971095/multi-threaded-evaluations-over-a-channel-in-julia)

---

<div class="post-metadata">

### Author: ![nielsls](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nielsls/32/18482_2.png) [@nielsls](https://discourse.julialang.org/u/nielsls)
#### Post date: [November 21, 2021, 8:45pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/15 "2021-11-21T20:45:05Z")

</div>

Yes, agree. Looking at the [Channel implementation](https://github.com/JuliaLang/julia/blob/35f0c911f443ae0d3933e1bce4321afd0f345aed/base/channels.jl#L32) it does indeed look thread-safe, e.g. with the use of locks for each put! and take! calls.  
The Julia docs could certainly need an upgrade here though.

---

<div class="post-metadata">

### Author: ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)
#### Post date: [November 21, 2021, 9:00pm UTC](https://discourse.julialang.org/t/any-issues-with-multiple-threads-writing-to-a-single-queue-which-is-consumed-by-another-thread/71846/16 "2021-11-21T21:00:44Z")

</div>

Watching this video as a result of all your help seems to me that it’s EXACTLY what I would like to do ( threaded workers) and the presenter is doing a marvelous job that even someone like me can grasp.

[Parallelizing Data Science with Julia](https://www.youtube.com/watch?v=8yxQ61Eiylw)
