How to make a global constant thread local?

To make my code thread-safe, I am making some constant thread local. However, in the following package example

module Foo

const bar = zeros(Threads.nthreads())

end

the length of bar will be stuck at JULIA_NUM_THREADS at the time of compilation.

What’s the correct way to make the constant bar thread local?

1 Like

You can create an empty array (or array of whatever size) where you define bar and then resize! it in an __init__() method (see here: https://docs.julialang.org/en/v1/manual/modules/index.html#Module-initialization-and-precompilation-1).

2 Likes