# Julia -\> C (Pthread) -\> Julia Saxpy with Threads

**URL:** <https://discourse.julialang.org/t/julia-c-pthread-julia-saxpy-with-threads/118097>\
**Category:** New to Julia\
**Tags:** ccall, multithreading, threads, threading\
**Created:** [August 12, 2024, 2:44pm UTC](https://discourse.julialang.org/t/julia-c-pthread-julia-saxpy-with-threads/118097 "2024-08-12T14:44:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![miniskar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miniskar/32/210144_2.png) [@miniskar](https://discourse.julialang.org/u/miniskar)\
**Post date:** [August 12, 2024, 2:44pm UTC](https://discourse.julialang.org/t/julia-c-pthread-julia-saxpy-with-threads/118097/1 "2024-08-12T14:44:50Z")

</div>

I am having an C++ library environment where I am supposed to call Julia kernel (example SAXPY) with threading.  
A minimum work example to illustrate the issue is given below.

The Julia main() function calls ‘call\_directly’ function written in ‘C’ to callback ‘saxpy\_openmp’ function written in Julia. It works good. However, when the Julia main() functions calls ‘call\_on\_thread’ function written in ‘C’ which creates a pthread and callback ‘saxpy\_openmp’ function written in Julia, it hangs during execution.

c\_mwe.c:

```julia
#include <julia.h>
#include <pthread.h>

typedef void (*julia_callback)();

void call_directly(julia_callback callback) {
    printf("Calling Julia directly\n");
    callback();
}

void *thread_function(void* callback) {
    printf("Calling Julia from thread\n");
    ((julia_callback)callback)();
    return NULL;
}
void call_on_thread(julia_callback callback) {
    jl_init();
    printf("Creating thread\n");
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, callback);
    pthread_join(thread, NULL);
}

```

julia\_mwe.jl:

```julia
export callback
using Base.Threads

function saxpy_openmp(Z, A, X, Y)
    n = length(X)
    @assert length(Y) == n "Vectors X and Y must have the same length"
    @assert length(Z) == n "Vectors X and Z must have the same length"
    Threads.@threads for i in 1:n
        Z[i] = A * X[i] + Y[i]
    end
end
function callback()::Cvoid
    println(Core.stdout, "Calling the GC")
    GC.gc()
    size = 10
    X = ones(size)
    Y = ones(size)
    Z = ones(size)
    saxpy_openmp(Z, 2.0, X, Y)
    println(Core.stdout, "Z: $Z")
    println(Core.stdout, "GC call done")
end
function main()
    callback_ptr = @cfunction(callback, Cvoid, ())
    ccall((:call_directly, "./wip.so"), Cvoid, (Ptr{Cvoid},), callback_ptr)
    println()
    gc_state = @ccall(jl_gc_safe_enter()::Int8)
    ccall((:call_on_thread, "./wip.so"), Cvoid, (Ptr{Cvoid},), callback_ptr)
    @ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
    println("Done")
end
main()

```

Instructions to compile:  
Makefile:

```julia
build:
    gcc -g -O0 -fPIC -shared -o wip.so c_mwe.c -I$(JULIA)/include/julia -L$(JULIA)/lib -ljulia -lpthread 
run:
    julia julia_mwe.jl

```

Please help in resolving this issue.

---

<div class="post-metadata">

**Author:** ![miniskar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miniskar/32/210144_2.png) [@miniskar](https://discourse.julialang.org/u/miniskar)\
**Post date:** [August 12, 2024, 4:49pm UTC](https://discourse.julialang.org/t/julia-c-pthread-julia-saxpy-with-threads/118097/2 "2024-08-12T16:49:28Z")

</div>

I thinks this issue when the number of threads is 1. I found a workaround to resolve this issue to not apply @threads when the number of threads is 1. I am closing this issue now.
