# Embedding Julia code in parallel C++

**URL:** <https://discourse.julialang.org/t/embedding-julia-code-in-parallel-c/68163>\
**Category:** General Usage\
**Tags:** parallel, c\
**Created:** [September 14, 2021, 6:38pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-parallel-c/68163 "2021-09-14T18:38:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 14, 2021, 6:38pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-parallel-c/68163/1 "2021-09-14T18:38:33Z")

</div>

Hey all- I’m working on a rather large machine learning project that entails using a C++ main to call functions written in Julia. In addition the regular pain of who manages what variables and how to translate between bitwise variable representations, one issue that has come up is that we plan to parallelize the C++ code. This will entail having multiple C++ threads, all of which need to call the same Julia precompiled library.

Is this possible? Is Julia re-entrant, for lack of a better word?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 14, 2021, 9:54pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-parallel-c/68163/2 "2021-09-14T21:54:42Z")

</div>

Linking [related post](https://discourse.julialang.org/t/calling-julia-functions-from-a-multi-threading-scheduler-written-in-c/54114/2).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 14, 2021, 10:53pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-parallel-c/68163/3 "2021-09-14T22:53:48Z")

</div>

The answer seems to be no, according to the linked post from the answer above, that is embedding two or more _instances_ of Julia not supported.

Before that link, I wanted to find out for myself, can be fun to see what you can figure out yourself, and to help you. There’s nothing definitive I could find in the docs, and looking at the source code is going down quite the rabbit-hole.

What I found, but not sure the meaning of:  
[https://github.com/JuliaLang/julia/search?q=jl\_init\_\_threading](https://github.com/JuliaLang/julia/search?q=jl_init__threading)

The docs state you should use jl\_init. jl\_init\_\_threading was deleted then brought back, so I’m very curious what it’s for:  
[https://github.com/JuliaLang/julia/commit/829d7e090f2cadfe1b1d506394f416b2938b3e7e#diff-cf6e417d01a6e25b814df855f594119c5d96891b39091abc802a5b6b04a86a56](https://github.com/JuliaLang/julia/commit/829d7e090f2cadfe1b1d506394f416b2938b3e7e#diff-cf6e417d01a6e25b814df855f594119c5d96891b39091abc802a5b6b04a86a56)

I’m pretty sure you would only initialize Julia once, and then it’s unclear where you go from there. Julia has threads now. There might be some workaround, other than letting Julia “drive”, which is supported, letting Julia take care of threading.

[Github tells me Julia is (partially) written in PHP. 🙂 That’s of course not true, it only detects PHP from the .inc file.]
