# Embedding Julia into multithreading apps

**URL:** <https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122>\
**Category:** Internals & Design\
**Created:** [January 26, 2019, 9:17am UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122 "2019-01-26T09:17:39Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 27, 2019, 3:08pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/21 "2019-01-27T15:08:40Z")

</div>

No, I didn’t mean from julia, but from C to Julia. Something like that: the user click on a button that dispatch a job to Julia thread and wait it to be finished.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 27, 2019, 3:11pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/22 "2019-01-27T15:11:56Z")

</div>

It depends on what exactly you do I guess?

I mean, if you use some code to do thread sync yourself and never run any julia code on your thread, julia certainly won’t care where the requests come from.

The code from @cdsousa [above](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/8) seems decent though I didn’t read it through carefully. The only obvious issue I’ll complain about is that julia does not support being initialize and finalize multiple times so make sure you don’t contruct that class more than once in the lifetime of the program.

---

<div class="post-metadata">

**Author:** ![Maxim\_Kuznetsov](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Maxim\_Kuznetsov](https://discourse.julialang.org/u/Maxim_Kuznetsov)\
**Post date:** [January 28, 2019, 12:40pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/23 "2019-01-28T12:40:17Z")

</div>

Thanks for the excellent code!  
The question remains - is it necessary to perform all the functions of Julia that way (via Worker)?  
or jl\_box\_xxx, jl\_unbox\_xxx families can be executed in parallel, without capturing the Workers` grand-lock ??

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [January 28, 2019, 2:23pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/24 "2019-01-28T14:23:01Z")

</div>

I actually don’t know that. I’ve just played a little with that code, and solely tried evaluating Julia code and nothing fancy from he Julia C API.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 28, 2019, 7:02pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/25 "2019-01-28T19:02:44Z")

</div>

Don’t call any function in another thread. For unbox, you can just load from the pointer directly without calling any functions.

---

<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:** [January 28, 2019, 7:22pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/26 "2019-01-28T19:22:19Z")

</div>

Have you looked at [AsyncCondition](https://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code/#Thread-safety-1)? I think it can be used for C-to-Julia communication (but not to “wait it to be finished”).

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 28, 2019, 9:49pm UTC](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/27 "2019-01-28T21:49:57Z")

</div>

Thanks! I will take a look 🙂

[Previous page](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122.md?page=1)
