# Segfault and crash embedding when julia runs multithreaded gc

**URL:** <https://discourse.julialang.org/t/segfault-and-crash-embedding-when-julia-runs-multithreaded-gc/75221>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [January 26, 2022, 10:37am UTC](https://discourse.julialang.org/t/segfault-and-crash-embedding-when-julia-runs-multithreaded-gc/75221 "2022-01-26T10:37:27Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [January 28, 2022, 9:42am UTC](https://discourse.julialang.org/t/segfault-and-crash-embedding-when-julia-runs-multithreaded-gc/75221/7 "2022-01-28T09:42:35Z")

</div>

I’ve been trying to get a simple multi-threaded example to crash on my end here, but no luck so far. For example:

```julia
// gcc -pthread -o t_threading `julia /usr/share/julia/julia-config.jl --cflags --ldflags --ldlibs` t_threading.c 
#include <julia/julia.h>
JULIA_DEFINE_FAST_TLS

void *func(void*)
{   
    jl_init();  
    jl_eval_string("println(Threads.nthreads())");
    jl_eval_string("x=[1]; @time Threads.@threads for i in 1:100 global x=hcat(x,size(rand(10000,1000))); end");
    jl_atexit_hook(0);
    
    return NULL;
}

int main() 
{
    pthread_t t;
    
    pthread_create(&t, NULL, func, NULL);
    pthread_join(t, NULL);
}

```

Note that there are some implied restrictions when embedding in a multi-threaded environment. The docs don’t mentioned these, but threads like [this one](https://discourse.julialang.org/t/embedding-julia-into-multithreading-apps/20122/10) suggest that calling Julia API functions from a thread that was not started by Julia is unsupported, and doing so could lead to all kinds of nasty behaviour. So to put simply, you should only call `jl_...` functions from either the thread in which `jl_init()` was called, or from a thread _that was started by Julia_ (and not from some other thread you started). Can you show some of your code to see what it does?

And does this segfault occur already the first time you run your plugin? Or does it take multiple runs?

---

_[View the full topic](https://discourse.julialang.org/t/segfault-and-crash-embedding-when-julia-runs-multithreaded-gc/75221)._
