# @cfunction within function, scoping problem

**URL:** https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484
**Category:** General Usage
**Tags:** question
**Created:** [August 16, 2021, 1:36pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484 "2021-08-16T13:36:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 16, 2021, 1:36pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/1 "2021-08-16T13:36:44Z")

</div>

Hi  
it seems that the macro @cfunction work only with function defined in global scope.  
Used within a function like

```julia
function get_ptr(f::Function)
    integrand_c = @cfunction( $f,Cdouble,(Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble}))
    ptr = Base.unsafe_convert(Ptr{Cvoid}, Base.cconvert(Ptr{Cvoid}, integrand_c))
    return ptr
end

```

will trigger `f not defined`.  
Changing `f` to `$f` seems to solve the problem, but then when using `ccall` my program hangs.  
Is there a proper way to use @cfunction on a julia callable within a function?

Thanks

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 16, 2021, 2:32pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/2 "2021-08-16T14:32:04Z")

</div>

[https://github.com/JuliaLang/julia/blob/master/test/ccall.jl#L797](https://github.com/JuliaLang/julia/blob/master/test/ccall.jl#L797) has tons of examples.

---

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 17, 2021, 9:24am UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/3 "2021-08-17T09:24:58Z")

</div>

thanks for the quick answer.  
But as far as i can see from the example , the only way to do what i would like to do, is to encapsulate the function parameter into local closure ( using `$f`) which in turn do not work in my case.  
The C function called via `ccall` hangs…  
Using the globally defined callable function works perfectly.  
So the call to

- `@cfunction ( callable ... ) ` and
- `@cfunction($callable ... )`

do not behave the same

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 19, 2021, 12:24pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/4 "2021-08-19T12:24:18Z")

</div>

> [@dbertini](#):
>
> The C function called via `ccall` hangs…

Why? That might be the root cause, but you just make the conclusion without providing any diagnostic info.

---

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 19, 2021, 12:55pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/5 "2021-08-19T12:55:09Z")

</div>

I don’t know why.  
The C function called via `ccall` is also multi-threaded. Using `callable` define on the global scope  
works perfectly, but using `@cfunction($callable...)` inside a function, the C called function hangs in a deadlock, probably a race condition.  
Is it allowed to call multithreaded C function with `ccall`. Or it there something one should be aware of?  
Which diagnostic info should i provide?

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 19, 2021, 12:57pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/6 "2021-08-19T12:57:24Z")

</div>

An MWE that can reproduce the problem.

---

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 19, 2021, 1:01pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/7 "2021-08-19T13:01:21Z")

</div>

For now i have no MWE, but i could try to make one…

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 19, 2021, 1:06pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/8 "2021-08-19T13:06:06Z")

</div>

`ccall` does have some problems if the C library calls the callback function in a different thread(see [https://github.com/JuliaAudio/PortAudio.jl/issues/56](https://github.com/JuliaAudio/PortAudio.jl/issues/56), [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Thread-safety)), but it looks like your case is a little bit different.

---

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 19, 2021, 2:33pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/9 "2021-08-19T14:33:09Z")

</div>

the called C function does not segfault because of incomplete thread safety support but end up in a deadlock … this is indeed a bit different.  
I can try to figure out where this exaclty happen …

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [August 31, 2021, 6:56pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/10 "2021-08-31T18:56:24Z")

</div>

From what I understand, you do currently to to call into Julia from different C-threads, @dbertini, correct?

---

<div class="post-metadata">

### Author: ![dbertini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbertini/32/24806_2.png) [@dbertini](https://discourse.julialang.org/u/dbertini)
#### Post date: [August 31, 2021, 7:09pm UTC](https://discourse.julialang.org/t/cfunction-within-function-scoping-problem/66484/11 "2021-08-31T19:09:59Z")

</div>

well, i do the following for the C interface:  
I translate a native user-defined Julia function via `@cfunction(callable)`  
After that an `optimize()` function is called that will use the converted C function  
from `@cfunction()` macro and execute internally `ccall`.  
When `callable` is defined on global scope and i convert the user-defined function  
with `@cfunction` before calling `optimize()` everything works fine.  
When i pass the native user function as parameter `optimize(f::Function)` and use then the macro  
with the closure `@cfunction( $f)` the `ccall` hangs in a deadlock.
