Konstantin, Thanks for the detailed explanation. That was helpful. On Thu, Oct 19, 2017 at 3:51 AM, Konstantin Khomoutov <kos...@bswap.ru> wrote:
> On Fri, Oct 13, 2017 at 12:23:42PM -0700, 'Pushkar' via golang-nuts wrote: > > > I need to call some C functions from Go. I think I know how to proceed > > using cgo. > > However, I was wondering how goroutines and blocking calls in the C > > functions work together. > > So in the below example (pseudocode) will the goroutine be suspended when > > the pthread_lock call is waiting to acquire the lock? > > > > I am guessing that the Go engine will detect that the OS thread on which > > the goroutine(s) are running is blocked so it will suspend them too. > > Once the thread is runnable, the blocked goroutines will be scheduled to > > run. > > To what Ian said, I would add only a single goroutine runs on a given > OS thread at any given time, and when a goroutine enters a cgo call, the > runtime makes sure that goroutine gets locked to the underlying OS > thread it's running on (as if the goroutine explicitly called > runtime.LockOSThread()). This makes sure both Go and C sides are > consistent about the thread their code executes on during the cgo call. > > Another thing to keep in mind is that the Go runtime scheduler gives a > goroutine which blocks in a syscall or a cgo calls about 20 us (as of Go > 1.9) to complete [1], and if it doesn't, the scheduler parks the > goroutine and may spawn another OS thread if there are runnable > goroutines -- to keep up with its promise to have runtime.GOMAXPROCS > goroutines running. > > A corollary to this is that if you make many such blocking cgo calls > roughly in parallel, this might lead to spawning many OS threads. > This is not a bad thing in itself, but it's something to keep in mind. > > 1. https://github.com/golang/go/issues/21827#issuecomment-329103577 > > -- pushkar -- 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. For more options, visit https://groups.google.com/d/optout.