# \#threads

**URL:** https://discourse.julialang.org/tag/threads/398.md?match_all_tags=true&page=2&tags%5B%5D=threads

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

**Page:** 3

---

## [Spurious allocations with @threads, when setting variables using if statements](https://discourse.julialang.org/t/spurious-allocations-with-threads-when-setting-variables-using-if-statements/82233)

<div class="topic-metadata">

**Author:** [@lukas.grunwald](https://discourse.julialang.org/u/lukas.grunwald)\
**Replies:** 5\
**Last updated:** [June 6, 2022, 12:45pm UTC](https://discourse.julialang.org/t/spurious-allocations-with-threads-when-setting-variables-using-if-statements/82233 "2022-06-06T12:45:27Z")

</div>

Annotating a for loop with @threads, that depends on a variable fixed in an if statement, leads to many additional allocations inside the loop. Replacing the if-statement with a ternary-operator removes these additional …

---

## [How to wait for a thread to complete](https://discourse.julialang.org/t/how-to-wait-for-a-thread-to-complete/78546)

<div class="topic-metadata">

**Author:** [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Replies:** 3\
**Last updated:** [March 27, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-wait-for-a-thread-to-complete/78546 "2022-03-27T19:56:10Z")

</div>

Hi all, I have a situation where I receive a message via ZMQ and would like to perform post processing of the reply on a separate thread and then send a reply from the main thread once the post processing is complete. I…

---

## [@threads fails when interating over a dictionary](https://discourse.julialang.org/t/threads-fails-when-interating-over-a-dictionary/78249)

<div class="topic-metadata">

**Author:** [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Replies:** 1\
**Last updated:** [March 22, 2022, 3:04am UTC](https://discourse.julialang.org/t/threads-fails-when-interating-over-a-dictionary/78249 "2022-03-22T03:04:36Z")

</div>

Trying to parallelise over a dictionary iterator fails. using Base.Threads d = Dict("a" =\> 1, "b" =\> 2) @threads for (k, v) in d println("$(k) =\> $(v)") end produces ERROR: TaskFailedException Stacktrace: \[1\] wai…

---

## [Parallel Reductions with Zygote](https://discourse.julialang.org/t/parallel-reductions-with-zygote/75969)

<div class="topic-metadata">

**Author:** [@SEA](https://discourse.julialang.org/u/SEA)\
**Replies:** 11\
**Last updated:** [February 9, 2022, 1:05pm UTC](https://discourse.julialang.org/t/parallel-reductions-with-zygote/75969 "2022-02-09T13:05:38Z")

</div>

I am trying to speed up the gradient computation of a model of the form sum(m-\>m(x), models), where models is an array of constituent models. This is (supposed to be) easily parallelizable, but I am having trouble maki…

---

## [Number of threads, puzzle](https://discourse.julialang.org/t/number-of-threads-puzzle/74965)

<div class="topic-metadata">

**Author:** [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Replies:** 9\
**Last updated:** [February 2, 2022, 10:06pm UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965 "2022-02-02T22:06:50Z")

</div>

draft.jl c1 = Channel(100) c2 = Channel(100) c3 = Channel(100) function task\_1() while true wait(c1) take!(c1) end end function task\_2() while true wait(c2) take!(c2) end…

---

## [Julia 1.7 says it can switch the thread your task is on. How often does that happen, and how can it be disabled?](https://discourse.julialang.org/t/julia-1-7-says-it-can-switch-the-thread-your-task-is-on-how-often-does-that-happen-and-how-can-it-be-disabled/75373)

<div class="topic-metadata">

**Author:** [@heyx3](https://discourse.julialang.org/u/heyx3)\
**Replies:** 6\
**Last updated:** [January 28, 2022, 5:01pm UTC](https://discourse.julialang.org/t/julia-1-7-says-it-can-switch-the-thread-your-task-is-on-how-often-does-that-happen-and-how-can-it-be-disabled/75373 "2022-01-28T17:01:19Z")

</div>

According to the release notes: Tasks can now migrate among threads when they are re-scheduled. Previously, a Task would always run on whichever thread executed it first Does this mean that the thread I’m running on …

---

## [Using Makie with Threads.@spawn](https://discourse.julialang.org/t/using-makie-with-threads-spawn/54618)

<div class="topic-metadata">

**Author:** [@tt1234567](https://discourse.julialang.org/u/tt1234567)\
**Replies:** 12\
**Last updated:** [January 23, 2022, 2:00pm UTC](https://discourse.julialang.org/t/using-makie-with-threads-spawn/54618 "2022-01-23T14:00:38Z")

</div>

I’d like to plot a stream of data in a separate thread. Using Makie works up to a point, but the axis labels seem to come out wrong and it occasionally crashes. I don’t know if I’m doing something wrong or if it’s a limi…

---

## [Julia Thread Affinity not persistent when calling MKL function](https://discourse.julialang.org/t/julia-thread-affinity-not-persistent-when-calling-mkl-function/74560)

<div class="topic-metadata">

**Author:** [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Replies:** 2\
**Last updated:** [January 14, 2022, 8:10pm UTC](https://discourse.julialang.org/t/julia-thread-affinity-not-persistent-when-calling-mkl-function/74560 "2022-01-14T20:10:45Z")

</div>

I observe a subtle issue where the pinning of Julia threads to specific cores is spoiled massively by running a seemingly harmless computation. By spoiled I mean that after running the computation all threads are pinned …

---

## [Julia 1.7 saving and restoring RNG state](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037)

<div class="topic-metadata">

**Author:** [@crinders](https://discourse.julialang.org/u/crinders)\
**Replies:** 3\
**Last updated:** [December 13, 2021, 9:09pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037 "2021-12-13T21:09:38Z")

</div>

Previously I checkpointed the RNG in a Julia session using multiple threads (40) with using JLD2 using Random jldopen(Filename,'w') do F F\["RNG"\] = Random.default\_rng()\` end and restored with using JLD2 using Rando…

---

## [Request for comments on approach to multithreaded work](https://discourse.julialang.org/t/request-for-comments-on-approach-to-multithreaded-work/72728)

<div class="topic-metadata">

**Author:** [@miguelbiron](https://discourse.julialang.org/u/miguelbiron)\
**Replies:** 2\
**Last updated:** [December 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/request-for-comments-on-approach-to-multithreaded-work/72728 "2021-12-08T23:00:35Z")

</div>

Hi, I’m developing a multithreaded version of a piece of software and already have a working solution. I just want to check if there is a simpler way of doing it. Basically, the problem can be reduced to this: suppose …

---

## [Write to text file while using threads](https://discourse.julialang.org/t/write-to-text-file-while-using-threads/63836)

<div class="topic-metadata">

**Author:** [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Replies:** 12\
**Last updated:** [November 24, 2021, 7:31pm UTC](https://discourse.julialang.org/t/write-to-text-file-while-using-threads/63836 "2021-11-24T19:31:01Z")

</div>

Hello everyone, I’m trying to use the Threads module. I have long running simulations that I want to run in parallel and save the data to a single file while the simulation is running. Here is what I have done so far: …

---

## [Does Julia detect the maximum number of threads availible and if so how?](https://discourse.julialang.org/t/does-julia-detect-the-maximum-number-of-threads-availible-and-if-so-how/71683)

<div class="topic-metadata">

**Author:** [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Replies:** 29\
**Last updated:** [November 19, 2021, 6:27pm UTC](https://discourse.julialang.org/t/does-julia-detect-the-maximum-number-of-threads-availible-and-if-so-how/71683 "2021-11-19T18:27:50Z")

</div>

New to julia and experimenting with threads. I can’t seem to figure out how Julia determines JULIA\_NUM\_THREADS I have two Intel xeon 2699 v3 in my hp z840 Intel blurb on 2699 v3 So what should JULIA\_NUM\_THREADS end…

---

## [Y.A.t.Q : Yet Another @threads Question](https://discourse.julialang.org/t/y-a-t-q-yet-another-threads-question/71541)

<div class="topic-metadata">

**Author:** [@kadir-gunel](https://discourse.julialang.org/u/kadir-gunel)\
**Replies:** 17\
**Last updated:** [November 18, 2021, 7:05am UTC](https://discourse.julialang.org/t/y-a-t-q-yet-another-threads-question/71541 "2021-11-18T07:05:21Z")

</div>

Hello, I am calculating the cosine distance between two vector spaces by using Distances.jl package. Since the vector spaces in comparison are large, I wanted to calculate cosine distances by using @threads. The proble…

---

## [Fastest possible trilinear interpolation with \`Interpolations.jl\`](https://discourse.julialang.org/t/fastest-possible-trilinear-interpolation-with-interpolations-jl/71184)

<div class="topic-metadata">

**Author:** [@Chiil](https://discourse.julialang.org/u/Chiil)\
**Replies:** 1\
**Last updated:** [November 9, 2021, 7:47am UTC](https://discourse.julialang.org/t/fastest-possible-trilinear-interpolation-with-interpolations-jl/71184 "2021-11-09T07:47:40Z")

</div>

I am working on developing a machine learning application in which two instances of our CFD code work simultaneously at different resolutions and data is passed between the two instances. For this, I need to have fastest…

---

## [Parallel and distributed very slow](https://discourse.julialang.org/t/parallel-and-distributed-very-slow/69735)

<div class="topic-metadata">

**Author:** [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Replies:** 6\
**Last updated:** [October 14, 2021, 12:36pm UTC](https://discourse.julialang.org/t/parallel-and-distributed-very-slow/69735 "2021-10-14T12:36:20Z")

</div>

Hi ! I am trying to learn how to distribute or use parellel computing on CPU, I don’t understand why, it is slower that a sequential for… As I really do not understand why, I ask for your help, thank you using Benchmar…

---

## [@threads uses only half the number of nthreads()](https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058)

<div class="topic-metadata">

**Author:** [@msekino](https://discourse.julialang.org/u/msekino)\
**Replies:** 10\
**Last updated:** [October 12, 2021, 12:58am UTC](https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058 "2021-10-12T00:58:07Z")

</div>

Recently I built a PC with Ryzen Threadripper 3990X, which has 64 cores and 128 threads. The OS is Windows 10 Pro. When I tried the code below, only 64 threads reached 100% as shown in the attached image. Why aren’t a…

---

## [Loosing performance with \`Threads.@threads\` for loop](https://discourse.julialang.org/t/loosing-performance-with-threads-threads-for-loop/69358)

<div class="topic-metadata">

**Author:** [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Replies:** 10\
**Last updated:** [October 7, 2021, 2:36pm UTC](https://discourse.julialang.org/t/loosing-performance-with-threads-threads-for-loop/69358 "2021-10-07T14:36:45Z")

</div>

I have strange performance issues when I use Threads.@threads to parallelize a for-loop. My machine runs on 40 cores: julia\> Threads.nthreads() 40 Now, I lose performance even with a quite simple example: N = 500 m = …

---

## [Do faster optimization with Cbc solver in JuMP](https://discourse.julialang.org/t/do-faster-optimization-with-cbc-solver-in-jump/46276)

<div class="topic-metadata">

**Author:** [@Luis\_Tormo](https://discourse.julialang.org/u/Luis_Tormo)\
**Replies:** 6\
**Last updated:** [September 23, 2021, 11:00pm UTC](https://discourse.julialang.org/t/do-faster-optimization-with-cbc-solver-in-jump/46276 "2021-09-23T23:00:44Z")

</div>

I am trying to do an optimization of a MILP problem with Cbc solver but it is too slow. I know that with CPLEX it is much faster but I need to use Cbc. Does anyone know of a resource to get it? Maybe multi-threading?

---

## [Strange deadlock in 1.7.0-beta3 didn't happen in 1.5.x](https://discourse.julialang.org/t/strange-deadlock-in-1-7-0-beta3-didnt-happen-in-1-5-x/66462)

<div class="topic-metadata">

**Author:** [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Replies:** 10\
**Last updated:** [August 16, 2021, 6:38pm UTC](https://discourse.julialang.org/t/strange-deadlock-in-1-7-0-beta3-didnt-happen-in-1-5-x/66462 "2021-08-16T18:38:02Z")

</div>

I have an Agents.jl simulation that worked fine last summer… I’m back to looking at the code, and it’s deadlocking and sitting there using 0% of cpu. It’s a script that uses Threads.@spawn to run some threads each of wh…

---

## [Copy an atomic variable as a regular variable](https://discourse.julialang.org/t/copy-an-atomic-variable-as-a-regular-variable/66117)

<div class="topic-metadata">

**Author:** [@matthiasbe](https://discourse.julialang.org/u/matthiasbe)\
**Replies:** 1\
**Last updated:** [August 10, 2021, 9:57am UTC](https://discourse.julialang.org/t/copy-an-atomic-variable-as-a-regular-variable/66117 "2021-08-10T09:57:38Z")

</div>

Hello, I am trying to read the value of an atomic (shared) variable as a local (on the current thread) variable. Here is what I tried. julia\> a = Threads.Atomic{Int}(2) Base.Threads.Atomic{Int64}(2) julia\> b = conver…

---

## [Error with parallel sparse array building](https://discourse.julialang.org/t/error-with-parallel-sparse-array-building/65833)

<div class="topic-metadata">

**Author:** [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Replies:** 0\
**Last updated:** [August 4, 2021, 5:46pm UTC](https://discourse.julialang.org/t/error-with-parallel-sparse-array-building/65833 "2021-08-04T17:46:54Z")

</div>

I know that the recommended way to @thread build array is to split the work based on nthreads() and then join the array later. (What I said is the same as the macro @threads.) Nevertheless, in the below each thread shoul…

---

## [Threads.spawn - increment/decrement atomic variable](https://discourse.julialang.org/t/threads-spawn-increment-decrement-atomic-variable/62502)

<div class="topic-metadata">

**Author:** [@Michal](https://discourse.julialang.org/u/Michal)\
**Replies:** 5\
**Last updated:** [June 7, 2021, 10:09am UTC](https://discourse.julialang.org/t/threads-spawn-increment-decrement-atomic-variable/62502 "2021-06-07T10:09:21Z")

</div>

This is my working code, which is spawning threads. This example is working. while true M = pullDataFromQueue() if !isnothing(M) Threads.@spawn begin # Worker dela…

---

## [Sparse Cholesky factorization with multiple threads](https://discourse.julialang.org/t/sparse-cholesky-factorization-with-multiple-threads/62327)

<div class="topic-metadata">

**Author:** [@Jeffrey](https://discourse.julialang.org/u/Jeffrey)\
**Replies:** 1\
**Last updated:** [June 3, 2021, 1:34pm UTC](https://discourse.julialang.org/t/sparse-cholesky-factorization-with-multiple-threads/62327 "2021-06-03T13:34:28Z")

</div>

Hi. I’m relatively new to Julia and I am trying to use multiple threads to speed up the sparse Cholesky factorization from SuiteSparse. I have a total of 32 physical cores available on the machine I’m using. When using …

---

## [What's the most efficient way to sum the results of multithreading?](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-sum-the-results-of-multithreading/61944)

<div class="topic-metadata">

**Author:** [@yingqiuz](https://discourse.julialang.org/u/yingqiuz)\
**Replies:** 3\
**Last updated:** [May 28, 2021, 10:23pm UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-sum-the-results-of-multithreading/61944 "2021-05-28T22:23:53Z")

</div>

Hi, I need to sum the outputs of each thread. Is there a standard/efficient way to do this? for example: X = Matrix{Float64}(d, N) Threads.@threads for k = 1:N X\[:, k\] .= some\_calculations() end sum!(ones(Int64, d…

---

## [Threads.@threads parallelism and htop](https://discourse.julialang.org/t/threads-threads-parallelism-and-htop/61762)

<div class="topic-metadata">

**Author:** [@cricci](https://discourse.julialang.org/u/cricci)\
**Replies:** 2\
**Last updated:** [May 25, 2021, 1:45pm UTC](https://discourse.julialang.org/t/threads-threads-parallelism-and-htop/61762 "2021-05-25T13:45:03Z")

</div>

Hello, on my machine I am observing the following behavior when running julia 1.6 and using DifferentialEquations.jl to solve a PDE. Basically I wrote a function f!(du,u,p,t) that uses the macro Threads.@threads insid…

---

## [Threads maxing out all cores, but no performance increase](https://discourse.julialang.org/t/threads-maxing-out-all-cores-but-no-performance-increase/58590)

<div class="topic-metadata">

**Author:** [@seulberg1](https://discourse.julialang.org/u/seulberg1)\
**Replies:** 16\
**Last updated:** [April 6, 2021, 3:06pm UTC](https://discourse.julialang.org/t/threads-maxing-out-all-cores-but-no-performance-increase/58590 "2021-04-06T15:06:41Z")

</div>

Hello All I started using Julia, because of its speed in an application, where the workload in pseudo-code looks like I added below (no MWE, but hopefully this is diagnostic enough - otherwise I will create a MWE, but i…

---

## [Threads.@threads with ONE thread: how to remove the overhead?](https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435)

<div class="topic-metadata">

**Author:** [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Replies:** 10\
**Last updated:** [April 2, 2021, 7:00pm UTC](https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435 "2021-04-02T19:00:17Z")

</div>

If there is only one thread set, the overhead of Threads.@threads is significant. julia\> Threads.nthreads() 1 julia\> function single\_thread(v) s = 0.0 @inbounds for i = 1:length(v) …

---

## [How to turn off @threads for ... while debugging](https://discourse.julialang.org/t/how-to-turn-off-threads-for-while-debugging/57234)

<div class="topic-metadata">

**Author:** [@asyrov](https://discourse.julialang.org/u/asyrov)\
**Replies:** 1\
**Last updated:** [March 15, 2021, 9:26pm UTC](https://discourse.julialang.org/t/how-to-turn-off-threads-for-while-debugging/57234 "2021-03-15T21:26:50Z")

</div>

I’d like to turn off (like it is nothing) @threads macro in some case while debugging. Such as I want to pass optional parameter use\_threads, and then how would I modify the following code not to use threads (possibly wi…

---

## [Getting multiple threads working in VS code, have changed envirinment settings.. no joy](https://discourse.julialang.org/t/getting-multiple-threads-working-in-vs-code-have-changed-envirinment-settings-no-joy/55076)

<div class="topic-metadata">

**Author:** [@PennyL](https://discourse.julialang.org/u/PennyL)\
**Replies:** 3\
**Last updated:** [February 12, 2021, 2:36pm UTC](https://discourse.julialang.org/t/getting-multiple-threads-working-in-vs-code-have-changed-envirinment-settings-no-joy/55076 "2021-02-12T14:36:41Z")

</div>

I followed the guidance in a previous thread, which led me to the environment settings in VS code where I have changed the thread number to 4, for user and workspace. However, julia is apparently still using one thread. …

---

## [Large ODE System Solving using @threads Crashes with Different Thread Counts](https://discourse.julialang.org/t/large-ode-system-solving-using-threads-crashes-with-different-thread-counts/50398)

<div class="topic-metadata">

**Author:** [@Jack\_hues](https://discourse.julialang.org/u/Jack_hues)\
**Replies:** 8\
**Last updated:** [January 12, 2021, 10:05pm UTC](https://discourse.julialang.org/t/large-ode-system-solving-using-threads-crashes-with-different-thread-counts/50398 "2021-01-12T22:05:37Z")

</div>

Hi, I’m currently working on solving a discretized PDE-ODE coupled system, which has been discretized into a system of odes where we then a noise in the form of a stochastic process, converting the ODE system into a sys…

[Previous page](https://discourse.julialang.org/tag/threads/398.md?match_all_tags=true&page=1&tags%5B%5D=threads)

[Next page](https://discourse.julialang.org/tag/threads/398.md?match_all_tags=true&page=3&tags%5B%5D=threads)
