# Using a shared library generated by PackageCompiler in a multi-threaded program

**URL:** https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972
**Category:** General Usage
**Tags:** package-compiler
**Created:** [November 18, 2025, 4:40pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972 "2025-11-18T16:40:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)
#### Post date: [November 18, 2025, 4:40pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/1 "2025-11-18T16:40:18Z")

</div>

I’m using PackageCompiler.jl to build a shared library, so that I can integrate one of my algorithms into a C++ simulation. The C++ simulation is multi-threaded. We’d like to call the Julia algorithm on a thread other than the main thread. When we call into the shared library from anything other than the main thread, though, it hangs.

Does the explanation in the [embedding docs](https://docs.julialang.org/en/v1/manual/embedding/#Thread-safety) apply to PackageCompiler, or does it have additional restrictions? If the embedding docs are appropriate, then it seems that as long as all of my calls to the shared library (including `init_julia`) happen on the worker thread, it should be fine. But like I said, the simulation hangs.

---

<div class="post-metadata">

### Author: ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)
#### Post date: [November 19, 2025, 6:33pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/2 "2025-11-19T18:33:00Z")

</div>

I ended up keeping all of my calls into the shared library on the main thread and handling the async on the Julia side. But I’d still love to learn more about what is possible with PackageCompiler.jl (and JuliaC.jl for that matter). Are they both required to run on the main thread? Is there documentation somewhere of these requirements/restrictions? If you can run it on a child thread, are there any examples to study?

---

<div class="post-metadata">

### Author: ![GeorgeGkountouras](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@GeorgeGkountouras](https://discourse.julialang.org/u/GeorgeGkountouras)
#### Post date: [November 20, 2025, 1:43pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/3 "2025-11-20T13:43:07Z")

</div>

PackageCompiler.jl in julia 1.11 has that limitation, while JuliaC.jl in julia 1.12 does not. Not sure about PackageCompiler.jl in julia 1.12.

Yes, it’s not well documented. Probably because JuliaC.jl is so new.

---

<div class="post-metadata">

### Author: ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)
#### Post date: [November 20, 2025, 2:01pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/4 "2025-11-20T14:01:14Z")

</div>

I have an old blog post on how to “adopt” multiple threads with PackageCompiler libraries, which is available since Julia 1.9: [Extreme Multi-Threading: C++ and Julia 1.9 Integration](https://scientificcoder.com/extreme-multi-threading-c-and-julia-19-integration).

I haven’t used this feature in a while. Also not tried it yet with JuliaC libraries (though I believe those should have all the functionalities of PackageCompiler.jl libraries, plus some more extra features)

---

<div class="post-metadata">

### Author: ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)
#### Post date: [November 24, 2025, 4:15pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/5 "2025-11-24T16:15:52Z")

</div>

Hi, can you reference the basis for your claim? The documentation on both, PackageCompiler and JuliaC, doesn’t mentioning the relation to the embedding section of the Julia manual.

---

<div class="post-metadata">

### Author: ![GeorgeGkountouras](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@GeorgeGkountouras](https://discourse.julialang.org/u/GeorgeGkountouras)
#### Post date: [November 25, 2025, 3:35pm UTC](https://discourse.julialang.org/t/using-a-shared-library-generated-by-packagecompiler-in-a-multi-threaded-program/133972/6 "2025-11-25T15:35:32Z")

</div>

This is based on personal tests with 1.11/1.12, and conversations with developers of core Julia. I did not try [calling the function 70 times](https://discourse.julialang.org/t/creating-fully-self-contained-and-pre-compiled-library/131374/36), but what used to crash immediately on `jl_init()` no longer does.

PackageCompiler should have (at least) the same limitations as regular Julia, so there isn’t anything to document there. The 1.12 change is part of some wider internal refactoring in the Julia compiler, but I’m afraid I cannot help you there.
