Cannot find a definitive answer, I suspect the answer is "yes" but would like a guarantee.
Is it true that a goroutine cannot switch a thread while it is calling a C function via CGO?
I.e. in a C function called from Go I can rely on the fact that it will execute in the same OS thread regardless of what it does (running for long, making blocking calls etc)?
Thanks.
Cannot find a definitive answer, I suspect the answer is "yes" but would like a guarantee.
Is it true that a goroutine cannot switch a thread while it is calling a C function via CGO?
I.e. in a C function called from Go I can rely on the fact that it will execute in the same OS thread regardless of what it does (running for long, making blocking calls etc)?
Thanks.
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Cannot find a definitive answer, I suspect the answer is "yes" but would like a guarantee.
Is it true that a goroutine cannot switch a thread while it is calling a C function via CGO?
I.e. in a C function called from Go I can rely on the fact that it will execute in the same OS thread regardless of what it does (running for long, making blocking calls etc)?
You can call runtime.LockOSThread to force a goroutine (and any C calls it makes) to be from one thread.
If I understood minux correctly, all calls into C are performed using
the same thread that was executing the goroutine. If you had more than
one goroutine active, then the Go runtime may spawn additional threads
to run those goroutines, while yours is in C. If those goroutines also
call foo_init and foo_err, then a mutex would be appropriate.
If the C code calls back into Go, the Go functions continue to run on
the same thread and cannot switch to any other (as though
runtime.LockOSThread() was called before the original cgo call). The
only exception to this is if the C code decides to run the callback
from another C thread.