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.