# Embedded julia in multithread c++: examples and doc

**URL:** <https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011>\
**Category:** New to Julia\
**Tags:** examples\
**Created:** [September 3, 2024, 9:54pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011 "2024-09-03T21:54:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [September 3, 2024, 9:54pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011/1 "2024-09-03T21:54:08Z")

</div>

Hi!  
Im searching for a scripting language which scripts could be run in different threads.  
No data share, only separate tasks. (with different math modules)  
Julia seems to be able to work this way, but i found non of examples.  
Only notes about `jl_adopt_thread`, and no explanation.

Which way i could learn julia embedding?

Is it good to use julia for embedding?

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [September 3, 2024, 10:23pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011/2 "2024-09-03T22:23:44Z")

</div>

As example, code from doc is not compiling!  
`double (*sqrt_jl)(double) = jl_unbox_voidpointer(jl_eval_string("@cfunction(sqrt, Float64, (Float64,))"));`  
But fix is obvious

---

<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 3, 2024, 11:28pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011/3 "2024-09-03T23:28:20Z")

</div>

This might help:

> **[Extreme Multi-Threading: C++ and Julia 1.9 Integration](https://scientificcoder.com/extreme-multi-threading-c-and-julia-19-integration)**
>
> A guide to weaving C++ threads into Julia with the latest thread adoption feature in Julia version 1.9

> Weaving threads across multiple programming languages is an extreme sport in software engineering. […]  
> `jl_adopt_thread` enables this C++ thread to be used by Julia. This is the most important C API function to remember for external multi-threading. [It’s available since Julia 1.9](https://github.com/JuliaLang/julia/blob/v1.9.0/NEWS.md#multi-threading-changes).

And using:  
[https://clemens-cords.com/jluna/](https://clemens-cords.com/jluna/)

> **[GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety,...](https://github.com/Clemapfel/jluna)**
>
> Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [September 3, 2024, 11:47pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011/4 "2024-09-03T23:47:16Z")

</div>

Thanks for respons!  
Try jluna but fall in non working multithreading - i even comment in existence issue on github, which designed in pre 1.9 way and didnt use `jl_adopt_thread`. Want to try to use older julia version to check origin of multithreading problem. Jluna seems incredibly promising in use, rather than pure api.

I also found the first one, but cant build this code due to lack of knowledge how (there is also missed files to include and some not compiled not documented functions mention in the post). Make another try to understand.

My hope is to directly study c api of julia to get minimal working example as starting point.

I even wrote one

```julia
JULIA_DEFINE_FAST_TLS

void thread(int (*test)(int), int count)
{
    jl_adopt_thread();

    double ret = test(count);

    std::cout << ret << std::endl;
}

int main(int argc, char *argv[])
{
    jl_init();

    jl_eval_string(R"(function Test(v::Int)
                        val = 0
                        for i in 1 : (v * 100)
                            val += i
                        end
                        return v
                      end)");

    int (*test)(int) = (int (*)(int)) jl_unbox_voidpointer(jl_eval_string("@cfunction(Test, Int, (Int,))"));

    std::thread all_threads[10];
    for(int i = 0; i < 10; ++i) {
        all_threads[i] = std::thread(thread, test, i);
    }

    // run all the threads
    for(auto& thread : all_threads)
        thread.join();

    return 0;
}

```

But there is no need in `jl_adopt_thread();` it still working. And not working with `print` or `sleep` functions.

Could that be `cfunction` interface function is easy to use in threads? (until they print or sleep in its body)?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 21, 2024, 2:18pm UTC](https://discourse.julialang.org/t/embedded-julia-in-multithread-c-examples-and-doc/119011/5 "2024-09-21T14:18:44Z")

</div>

> [@Sergey](#):
>
> jl\_adopt\_thread

`jl_adopt_thread` is for adopting foreign threads from outside Julia. You do not need this if you initialize Julia with it’s own thread pool, for example via the `-t` command line option. You can also look up the `JLOptions` structure for a similar facility.

[https://docs.julialang.org/en/v1/manual/multi-threading/](https://docs.julialang.org/en/v1/manual/multi-threading/)
