cgo and OSX "main thread"

141 views
Skip to first unread message

Alex Flint

unread,
Sep 9, 2016, 1:51:45 PM9/9/16
to golang-nuts
I am using cgo to wrap a library that creates an OSX UI, which requires things to be run on the "main thread". Sometimes my cgo calls end up on the main thread and sometimes they do not (I check this with [NSThread isMainThread]). I understand that cgo threads are guaranteed to run on an OS-allocated stack, but what does this mean w.r.t. the OSX "main thread"? Has anyone else faced this problem before?

Ian Lance Taylor

unread,
Sep 9, 2016, 1:57:24 PM9/9/16
to Alex Flint, golang-nuts
See https://golang.org/pkg/runtime/#LockOSThread . The goroutine that
runs the `main` function should be the main thread, so if you call
`LockOSThread` first thing and then do all your cgo calls from that
goroutine everything should be fine.

Ian

Martin Bertschler

unread,
Sep 9, 2016, 6:02:51 PM9/9/16
to golang-nuts
There is also a great wiki article on how to execute functions on the main thread from multiple goroutines. It helped me a lot when I did some GUI development experiments.
https://github.com/golang/go/wiki/LockOSThread
Reply all
Reply to author
Forward
0 new messages