# Embedding Julia and multiple foreign threads

**URL:** <https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394>\
**Category:** General Usage\
**Tags:** question, multithreading\
**Created:** [December 3, 2024, 7:43am UTC](https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394 "2024-12-03T07:43:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peremato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peremato/32/29128_2.png) [@peremato](https://discourse.julialang.org/u/peremato)\
**Post date:** [December 3, 2024, 7:43am UTC](https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394/1 "2024-12-03T07:43:36Z")

</div>

The embedding [documentation](https://docs.julialang.org/en/v1/manual/embedding/#Thread-safety) says clearly that you can only use the functions `jl_...()` from a single foreign thread in which you have called `jl_init()`. The question is, has anything changed with respect this restriction in view of the introduction in 1.9 of `jl_adopt_thread()`. Is a thread started by Julia equivalent to a foreign thread that has called `jl_adopt_thread()`? Thanks very in advance.

---

<div class="post-metadata">

**Author:** ![peremato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peremato/32/29128_2.png) [@peremato](https://discourse.julialang.org/u/peremato)\
**Post date:** [December 4, 2024, 10:48am UTC](https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394/2 "2024-12-04T10:48:11Z")

</div>

I have been experimenting. Calling `jl_adopt_thread()` in each foreign thread it allows me to call Julia functions in all threads without crashes and producing the expected results. Of course, the no observation of crashes does not imply that it would work correctly in more complicated cases.

---

<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:** [December 4, 2024, 1:30pm UTC](https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394/3 "2024-12-04T13:30:46Z")

</div>

I am also very interested in this feature since it will open a lot of possibilities for creating GUI applications using Julia as the back end.

---

<div class="post-metadata">

**Author:** ![peremato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peremato/32/29128_2.png) [@peremato](https://discourse.julialang.org/u/peremato)\
**Post date:** [December 4, 2024, 1:51pm UTC](https://discourse.julialang.org/t/embedding-julia-and-multiple-foreign-threads/123394/4 "2024-12-04T13:51:14Z")

</div>

The example I have been working is for the [Geant4.jl](https://github.com/JuliaHEP/Geant4.jl) package. The use case is for HEP experiments that do have a C++ simulation application already running in which all the callbacks are in C++, and they would like to replace them (or a least some part) with Julia functions to benefit from the Julia ecosystem and be more dynamic than C++. Here is the [example](https://juliahep.github.io/Geant4.jl/dev/examples/JuliaAction/) I have been playing if you are interested.
