# How to do a timedwait on a ReentrantLock or Threads.Condition

**URL:** <https://discourse.julialang.org/t/how-to-do-a-timedwait-on-a-reentrantlock-or-threads-condition/97885>\
**Category:** General Usage\
**Created:** [April 25, 2023, 1:02am UTC](https://discourse.julialang.org/t/how-to-do-a-timedwait-on-a-reentrantlock-or-threads-condition/97885 "2023-04-25T01:02:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryaan/32/36858_2.png) [@bryaan](https://discourse.julialang.org/u/bryaan)\
**Post date:** [April 25, 2023, 1:02am UTC](https://discourse.julialang.org/t/how-to-do-a-timedwait-on-a-reentrantlock-or-threads-condition/97885/1 "2023-04-25T01:02:18Z")

</div>

Is it possible to do a timedwait on a ReentrantLock or Threads.Condition?

```julia
rlock = ReentrantLock()

timedwait(rlock, 5)

```

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [April 25, 2023, 9:10am UTC](https://discourse.julialang.org/t/how-to-do-a-timedwait-on-a-reentrantlock-or-threads-condition/97885/2 "2023-04-25T09:10:44Z")

</div>

I suppose you could do something like that:

```julia
struct CheckOnLock
    lck::ReentrantLock
end

# the `function (foo::Bar)(args...; kwargs...) ... end` syntax creates a
# method that is called upon calling `foo(args...; kwargs...)`, where `foo` is
# a value of type `Bar`
function (x::CheckOnLock)()
    return !islocked(x.lck)
end

timedwait(CheckOnLock(rlock), 5)

```

If you want to keep the lock when `timedwait` succeeds, then you should use the function:

```julia
(x::CheckOnLock)() = trylock(x.lck)

```

instead of the `function (x::CheckOnLock)() ...` above.
