# Multithreading, preallocation of objects, threadid() and task migration

**URL:** <https://discourse.julialang.org/t/multithreading-preallocation-of-objects-threadid-and-task-migration/67640>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [September 3, 2021, 11:58am UTC](https://discourse.julialang.org/t/multithreading-preallocation-of-objects-threadid-and-task-migration/67640 "2021-09-03T11:58:38Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [September 3, 2021, 7:01pm UTC](https://discourse.julialang.org/t/multithreading-preallocation-of-objects-threadid-and-task-migration/67640/6 "2021-09-03T19:01:19Z")

</div>

For example, consider:

```julia
function do_stuff!(matrix, x)
    matrix[1, 1] = a = rand()
    yield() # or `@show`, or `@debug`, or write to a file, ...
    matrix[1, 1] += x
    @assert matrix[1, 1] == a + x # this *can* fail
end

```

The Julia runtime may decide to suspend a task executing `do_stuff!(matrices[1], x)` when it hits `yield` and starts a new task at thread 1, executing `do_stuff!(matrices[1], x)` again but with different `x`. Then, by the time the original task is restarted, `matrix[1, 1]` has a completely different value.

(For similar discussion, see [Is `@async` memory safe? - #8 by tkf](https://discourse.julialang.org/t/is-async-memory-safe/66558/8). This is about `@async` but task migration occurs at yield points and so it explains why `matrices[Threads.threadid()]` style does not work in general.)

So, the only way to use the `matrices[Threads.threadid()]` style is to make sure that there is no I/O (yield points) in _all code reachable_ from `do_stuff!`. Sometimes it’s doable, but it does not scale well. In particular, `do_stuff!` cannot be generic over its argument anymore since you don’t know what methods of the functions are going to be used. This is why I’m against `matrices[Threads.threadid()]` style.

> [@Bruno\_Amorim](#):
>
> there should be a solution for this problem in Base

Yeah, like putting FLoops in stdlib? :troll:

---

_[View the full topic](https://discourse.julialang.org/t/multithreading-preallocation-of-objects-threadid-and-task-migration/67640)._
