On Mon, Feb 15, 2021 at 1:32 AM changkun <h...@changkun.de> wrote:
>
> I would like to call a C function from Go and get notified about the 
> execution status in Go, say a goroutine wait until the C function finally 
> made some progress.
> Initially, I thought about passing a channel to C but the cgo document does 
> not say anything about that:
>
> Later, I am thinking about using an atomic variable to sync status according 
> to its value, but somehow I end up with this warning while I am compiling a 
> cgo program:
>
> package main
>
> /*
> #include <stdatomic.h>
> void ainit(atomic_int *val) {
>     atomic_init(val, 0);
> }
> */
> import "C"
>
> func main() {
>     var v C.atomic_int
>     C.ainit(&v)
> }
>
> cgo-gcc-prolog: In function ‘_cgo_8a67e594de48_Cfunc_ainit’:
> cgo-gcc-prolog:49:14: warning: passing argument 1 of ‘ainit’ from 
> incompatible pointer type [-Wincompatible-pointer-types]
> ./main.go:5:24: note: expected ‘_Atomic atomic_int *’ {aka ‘_Atomic int *’} 
> but argument is of type ‘int *’
>     5 | void ainit(atomic_int *val) {
>       |            ~~~~~~~~~~~~^~~
>
> According to the warning and note, it seems that cgo is lacking translating 
> atomic_init? Did I do anything wrong? Or is there any better and preferred 
> way to get notified from C function?

Even if there were a way to do this, an atomic variable is not a good
synchronization mechanism, because the other side has to poll the
variable.  You can do this as a last resort, but it doesn't sound like
you are at a last resort here.  I suggest that you have your C
function call a Go function to write a value on a channel.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcU7UFk%2BecZcRNOgA8vspQx%3D2rvTSSGmYpF2kD%2Bm_UqUPQ%40mail.gmail.com.

Reply via email to