# Multithreading for recursive operations

**URL:** https://discourse.julialang.org/t/multithreading-for-recursive-operations/26020
**Category:** Internals & Design
**Tags:** multithreading
**Created:** [July 4, 2019, 6:12pm UTC](https://discourse.julialang.org/t/multithreading-for-recursive-operations/26020 "2019-07-04T18:12:56Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [July 5, 2019, 4:53am UTC](https://discourse.julialang.org/t/multithreading-for-recursive-operations/26020/2 "2019-07-05T04:53:19Z")

</div>

On current master (v1.3-DEV), tasks are run on threads. Using a macro offered by Jeff Bezanson and reposted in

> [@Julia and Concurrency](https://discourse.julialang.org/t/julia-and-concurrency/25556/5):
>
> Just to add, I believe this macro was shared in Slack as an easy way to try out the functionality: macro par(expr) thunk = esc(:(()-\>($expr))) quote local task = Task($thunk) task.sticky = false schedule(task) task end end

one can get what you describe:

```julia
right_task = @par do_stuff!(res_right, right)
do_stuff!(res_left, left)
wait(right_task)

```

In my experiments so far I find that there is moderate overhead and one has to be diligent about inference (function barriers are often helpful). But it works, and I see good scaling (e.g. a basic recursive task-decomposed merge-sort of Float64 on 1 core is 1.6x slower than the serial version in Base, but running on 8 cores speeds up the tasked version by 6x).

Data dependencies, task priorities, and cache-friendly algorithm design are obviously still up to the programmer (for now).

I’ve learned enough about concurrent programming to be full of admiration for the PARTR implementors, principally Kiran, Jameson, and Jeff IIUC. This is really nice infrastructure.

---

_[View the full topic](https://discourse.julialang.org/t/multithreading-for-recursive-operations/26020)._
