On Tue, Jun 20, 2023 at 5:32 AM Bảo Phan Quốc <phq...@gmail.com> wrote:
>
> I'm using cgo to call a C function from Go. Inside the C function there is a 
> callback to a Go function. In other way, I'm calling Go -> C -> Go.
>
> After running pprof, I noticed that the __GI___pthread_mutex_unlock took half 
> of the execution time. AFAIK, cgo has an overhead, especially calling back 
> from C to Go. But it's weird that cgo takes half of the execution time to do 
> some locking. Is there something wrong in my code?

Every call from C to Go does acquire a mutex to check that the Go
runtime has been fully initialized.  This is normally not a big deal
as the mutex is held only briefly.  But your code does a lot of
parallel calls from C to Go.  It is possible that the heavy contention
on the mutex is causing the results that you are seeing.

The code in question is the C function _cgo_wait_runtime_init_done in
runtime/gco/gcc_libinit.c.  It might be possible to speed it up in
some way to avoid the mutex in the normal case.

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/CAOyqgcU7Csb_hf5gvy8dVHKEHd8GFkN-fUML18uW0HiQGOAXyg%40mail.gmail.com.

Reply via email to