php editor Zimo found that when using cgo, sometimes you will encounter a problem, that is, `__GI___pthread_mutex_unlock` takes up most of the execution time. This problem may cause performance degradation and affect the running efficiency of the program. In order to solve this problem, we need to deeply understand the function and principle of `__GI___pthread_mutex_unlock` and find the corresponding optimization method. This article will provide a detailed analysis of this problem to help readers better understand and solve this problem.
I am using cgo to call a c function from go. There is a callback to the go function inside the c function. In other words, I call go -> c -> go.
After running pprof, I noticed that __gi___pthread_mutex_unlock
took up half of the execution time. Afaik, cgo has overhead, especially callbacks from c to go. But strangely, cgo spends half of its execution time doing some locking. Is there something wrong with my code?
main.go
callback.go
Running environment:
lscpu
go language version
65bcae9650aaThis is the pprof result:
Edit: Add running environment
Although I can't reproduce it with the above program:
65bceaa20ecfBut there is a global mutex for each callback, so if you do parallel callbacks, you'll lose performance.
The above is the detailed content of go - `__GI___pthread_mutex_unlock` takes most of the execution time when using cgo. For more information, please follow other related articles on the PHP Chinese website!