# Dicts in different @async blocks

**URL:** <https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 15, 2020, 1:38pm UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640 "2020-04-15T13:38:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 15, 2020, 1:38pm UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/1 "2020-04-15T13:38:43Z")

</div>

It’s this safe? Using `Dict` async in the same thread?

```julia
d = Dict()

@async do_something_with_d(d)

@async do_something_else_with_d(d)

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 15, 2020, 3:12pm UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/2 "2020-04-15T15:12:57Z")

</div>

Maybe something can go wrong when you have I/O in `hash` or `isequal` of custom key type? It’s not nice to make them inpure but it’s conceivable for someone to put `@debug` statements in these functions.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [April 17, 2020, 4:45am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/3 "2020-04-17T04:45:45Z")

</div>

I don’t know for sure but probably not. You may want to use a Lock to synchronize access.

P.S. Last I asked about how to determine whether something is thread safe. The answer is to test it out vigorously and see if it crashes. It would be good if the documentation spells out clearly.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 17, 2020, 5:52am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/4 "2020-04-17T05:52:12Z")

</div>

The question was if `Dict` is safe to use concurrent (but not parallel) tasks started via `@async`. `Dict` is obviously not “thread-safe” (“`@spawn`/`@threads`-safe”).

Though I agree that it’s a good idea to use a lock in this case anyway if you _really_ need simultaneous access to `Dict`. (Even better and often much more efficient approach is to restructure your program so that you don’t need to use raw lock or atomics.)

> [@tk3369](#):
>
> The answer is to test it out vigorously and see if it crashes.

No offense, but I believe this is a very dangerous attitude towards thread programming. In principle, you should be able to _prove_ that that your program does not have data race given the public API definitions of the functions you are using. The behavior of your program is undefined otherwise. But I agree that documentation is lacking. Even basic thread programming constructs like `Channel` do not explicitly mention what operations are thread-safe in the docstring.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 17, 2020, 8:07am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/5 "2020-04-17T08:07:35Z")

</div>

Is there a lot of overhead associated with locks?

Which kind of lock should I use?  
(I know: it depends. One concurrent task has to access a couple of 1000 times per second and the other task every so often)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 18, 2020, 5:34am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/6 "2020-04-18T05:34:37Z")

</div>

I think Julia only has `ReentrantLock` and `Threads.SpinLock` ATM. You can look at [`Threads.SpinLock` docstring](https://docs.julialang.org/en/v1/base/multi-threading/#Base.Threads.SpinLock) for its caveats. But I think `ReentrantLock` is the first goto solution anyway. Or are you talking about implementing locks yourself?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [April 20, 2020, 1:44am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/7 "2020-04-20T01:44:57Z")

</div>

> [@tkf](#):
>
> No offense, but I believe this is a very dangerous attitude towards thread programming. In principle, you should be able to _prove_ that that your program does not have data race given the public API definitions of the functions you are using. The behavior of your program is undefined otherwise. But I agree that documentation is lacking. Even basic thread programming constructs like `Channel` do not explicitly mention what operations are thread-safe in the docstring.

@tkf I am in 100% agreement with you. Documentation makes a difference. Or the API should be clear e.g. thread-safe data structures could carry a prefix like `TS` (abbreviation of thread-safe for lack for a better suggestion) so it’s easy to determine if it’s the right data structure to use for writing thread-safe programs.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 22, 2020, 7:59am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/8 "2020-04-22T07:59:39Z")

</div>

No I don’t want to implement lock by myself. I tried Semaphore because this way awaiting tasks are waiting in a queue and will be notified in a FIFO manner.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 23, 2020, 12:33am UTC](https://discourse.julialang.org/t/dicts-in-different-async-blocks/37640/9 "2020-04-23T00:33:03Z")

</div>

If semaphore works for your case then I suppose that’s good? Though semaphore (at least Julia’s) doesn’t have the FIFO semantics. You can use channel if you want FIFO.
