Task seems to lock but not unlock mutex

If you run this code

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:

 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?

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.

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.

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)