I forgot to mention that the main goroutine in the test program is locked to a thread as I call runtime.LockOSThread() in init()

Greets,

On Mon, Feb 13, 2017 at 6:57 AM, Luka Napotnik <l...@zeta.si> wrote:
Thanks for the reply. I've created a test program with function F1 that calls a C function F2. The function F2 then calls a Go function F3.

I've started the test once with GOMAXPROCS set to 1, and the second time without an env. (using Go 1.7). There are short pauses between calls and I've measured the number of system threads used by the process with ps -o nlwp.

I found that when running the test with GOMAXPROCS=1, a new thread is always spawned when the C function F2 calls a Go function, but not if the env. is not set. Why is that? I know GOMAXPROCS can be ignored in some cases, so I assumed the GO runtime needed an extra thread for executing function F3, after it was called from C.

Greets,

On Mon, Feb 13, 2017 at 6:17 AM, Ian Lance Taylor <i...@golang.org> wrote:
On Sun, Feb 12, 2017 at 1:47 AM,  <l...@zeta.si> wrote:
Hello. I have a Go program that calls C functions in order to create a Gtk+ GUI. Because Gtk+ itself isn't thread-safe, I'm wondering if it's even possible to write GUI apps this way since there's no guarantee that the same thread will be used to call the API (even if I lock the main goroutine
 inside a OS thread). Am I correct?

You have to arrange to make all Gtk calls from a single goroutine, and
you have to make that single goroutine call runtime.LockOSThread
before it makes any Gtk calls.  Making all the calls from a single
goroutine typically means that that goroutine has to sit waiting on a
channel that other goroutines use to tell it what to do.  This
approach should be reliable.

Also, what happens if the called CGO function calls a Go function? Can it happen that the Go runtime will create a new thread, because the previous
 one is still locked?

If a Go function F1 calls a C function and the C function (running in
the same C thread) calls a Go function F2, F2 will run in the same
thread as F1. This is true whether or not F1 is locked to the thread.
In fact, for the duration of the C function, the goroutine will be
locked to the thread anyhow.

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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to