# Segmentation fault on threadcall of cfunction from Julia function

**URL:** <https://discourse.julialang.org/t/segmentation-fault-on-threadcall-of-cfunction-from-julia-function/19707>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [January 16, 2019, 3:44pm UTC](https://discourse.julialang.org/t/segmentation-fault-on-threadcall-of-cfunction-from-julia-function/19707 "2019-01-16T15:44:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stemann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stemann/32/4030_2.png) [@stemann](https://discourse.julialang.org/u/stemann)\
**Post date:** [January 16, 2019, 3:44pm UTC](https://discourse.julialang.org/t/segmentation-fault-on-threadcall-of-cfunction-from-julia-function/19707/1 "2019-01-16T15:44:23Z")

</div>

Hi,

I have a blocking function in a C++ library that I have wrapped using CxxWrap. I would like to start a thread calling this blocking function, so no Julia thread is blocked, using uv\_async\_send to signal a Julia async task whenever the blocking C++ function completes.

I have tried keeping as much code as possible in Julia, by writing a Julia function `blocking_fn` calling `uv_async_send`, obtaining a `cfunction`, and passing this to `@threadcall` - unfortunately this segfaults:

```julia
function blocking_fn(handle::Ptr{Nothing})
    uv_async_send_result = ccall(:uv_async_send, Cint, (Ptr{Nothing}, ), handle)
    return uv_async_send_result
end

cond = Base.AsyncCondition()

@sync begin
    @async begin
        for i = 1:3
            println("T1 Waiting")
            wait(cond)
            println("T1 Processing")
            sleep(0.1)
        end
    end
    @async begin
        for i = 1:3
            println("T2 Waiting")
            sleep(0.1)
            println("T2 Ready")
            # @threadcall(:uv_async_send, Cint, (Ptr{Nothing}, ), cond.handle)
            let blocking_fn_c = @cfunction(blocking_fn, Cint, (Ptr{Nothing},))
                GC.@preserve blocking_fn_c @threadcall(Base.unsafe_convert(Ptr{Cvoid}, blocking_fn_c), Cint, (Ptr{Nothing},), cond.handle)
            end
        end
    end
end

```

`@threadcall`’ing `uv_async_send` directly seems to work.

Is this just something which have to wait for the parallel task runtime (PTR) to be done?

See also [Equiavalent of @threadcall but for Julia code?](https://discourse.julialang.org/t/equiavalent-of-threadcall-but-for-julia-code/1335)
