# Is it safe declare lock as module constant?

**URL:** https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507
**Category:** General Usage
**Tags:** question, package, multithreading, threads
**Created:** [February 12, 2023, 11:13am UTC](https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507 "2023-02-12T11:13:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![harsh\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harsh_kumar/32/46042_2.png) [@harsh\_kumar](https://discourse.julialang.org/u/harsh_kumar)
#### Post date: [February 12, 2023, 11:13am UTC](https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507/1 "2023-02-12T11:13:00Z")

</div>

I have created a package that uses a spin lock and I have declared this as module constant (example shown below). Since Julia compiles the package, it will declare the constant only once. Will this cause any unexpected issues? If it is safe, can I use Reentract Lock, Thread Condition in similar way?

```julia
module Test

import Base.Threads: SpinLock, lock, unlock, trylock, islocked, wait, notify
const spin_lock = SpinLock()

function f1()
    lock(spin_lock) do 
        # some work
    end
end

function f2()
    lock(spin_lock) do 
        # some work
    end
end
end

```

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [February 12, 2023, 1:41pm UTC](https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507/2 "2023-02-12T13:41:33Z")

</div>

Yep, it works great! It’s a pretty common pattern among packages, from what I’ve seen.

---

<div class="post-metadata">

### Author: ![harsh\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harsh_kumar/32/46042_2.png) [@harsh\_kumar](https://discourse.julialang.org/u/harsh_kumar)
#### Post date: [February 13, 2023, 10:43am UTC](https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507/3 "2023-02-13T10:43:29Z")

</div>

Can you share some package that uses lock in this way? Probably it will help me in following common practice

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [February 13, 2023, 4:03pm UTC](https://discourse.julialang.org/t/is-it-safe-declare-lock-as-module-constant/94507/4 "2023-02-13T16:03:18Z")

</div>

AMDGPU.jl: [AMDGPU.jl/AMDGPU.jl at 43133cd2e83b66feb52fc8373cfcc0714dcd3810 · JuliaGPU/AMDGPU.jl · GitHub](https://github.com/JuliaGPU/AMDGPU.jl/blob/43133cd2e83b66feb52fc8373cfcc0714dcd3810/src/AMDGPU.jl#L71)  
Dagger.jl: [Dagger.jl/Sch.jl at 0107b31ce422e5a4fa5b15ab873b34d4c07306f2 · JuliaParallel/Dagger.jl · GitHub](https://github.com/JuliaParallel/Dagger.jl/blob/0107b31ce422e5a4fa5b15ab873b34d4c07306f2/src/sch/Sch.jl#L281)

Neither of these are particularly simple packages, but for the most part, it works as you’d expect: the lock is global across the Julia process.
