# Task seems to lock but not unlock mutex

**URL:** <https://discourse.julialang.org/t/task-seems-to-lock-but-not-unlock-mutex/117460>\
**Category:** General Usage\
**Tags:** mutex\
**Created:** [July 25, 2024, 9:48am UTC](https://discourse.julialang.org/t/task-seems-to-lock-but-not-unlock-mutex/117460 "2024-07-25T09:48:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [July 25, 2024, 9:48am UTC](https://discourse.julialang.org/t/task-seems-to-lock-but-not-unlock-mutex/117460/1 "2024-07-25T09:48:52Z")

</div>

If you run this code

```julia
using Base.Threads

# mylock = ReentrantLock()

btask = @async while true
    # global mylock
    # lock(mylock)
    print("B\n")
    sleep(4/5)
    # unlock(mylock)
end

atask = @async while true
    # global mylock
    # lock(mylock)
    print("A")
    sleep(1/2)
    # unlock(mylock)
end

```

You get this output:

```julia
 AB
AAB
AB
AAB
AB
AAB
AAB
AB
AAB
AB

```

Which is just right. If I uncomment the commented lines to add a mutex logic, it should result in `AB AB AB`. But what I get is just `BBBBBB`. So it looks like the first task is locking the lock, but then the second task can never lock it, as if it were never unlocked. What am I getting wrong?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 25, 2024, 10:33am UTC](https://discourse.julialang.org/t/task-seems-to-lock-but-not-unlock-mutex/117460/2 "2024-07-25T10:33:09Z")

</div>

> [@xor0110](#):
>
> ```julia
> btask = @async while true
> # global mylock
> # lock(mylock)
> print("B\n")
> sleep(4/5)
> # unlock(mylock)
> end
> 
> ```

Here the `unlock` is followed immediately by `lock` again on the next loop iteration (and without anything yielding like IO or sleep), so the second task never has a chance to acquire the lock.

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [July 25, 2024, 11:05am UTC](https://discourse.julialang.org/t/task-seems-to-lock-but-not-unlock-mutex/117460/3 "2024-07-25T11:05:11Z")

</div>

That makes a lot of sense, thanks. I need to familiarize myself with other signaling mechanisms, I think I got it working now using `Condition`.

```julia
using Base.Threads

# mylock = ReentrantLock()
mylock = Condition()

btask = @async while true
    global mylock

    wait(mylock)
    print("B\n")
    sleep(4/5)
    notify(mylock)
end

atask = @async while true
    global mylock

    wait(mylock)
    print("A")
    sleep(1/2)
    notify(mylock)
end

notify(mylock)

```
