Home > Backend Development > Golang > go - `__GI___pthread_mutex_unlock` takes most of the execution time when using cgo

go - `__GI___pthread_mutex_unlock` takes most of the execution time when using cgo

WBOY
Release: 2024-02-10 09:45:08
forward
487 people have browsed it

go - 使用 cgo 时,`__GI___pthread_mutex_unlock` 占用大部分执行时间

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.

Question content

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

65bcae965051

callback.go

65bcae965088

Running environment:

lscpu

65bcae965093

go language version

65bcae9650aa

This is the pprof result:

Edit: Add running environment

Workaround

Although I can't reproduce it with the above program:

65bceaa20ecf

But 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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template