New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: runtime: add RunOnMainThread to take control of the main thread #64777
Comments
The combination of "It panics if called outside an init function" and "[it panics] if called more than once" is rather unfortunate. If a project happen to (perhaps transitively) pull in two packages that both might need main thread functionality, but the project doesn't actually need that functionality from both packages, they're now in a pickle because the mere act of importing the package must run init functions, and those must call RunOnMainThread if there's any chance the package may need to use the main thread. |
Here's a neat variant that you may like better: // RunOnOSThread runs the function in a new goroutine
// wired to the thread of the caller. If the caller has locked
// the thread with `LockOSThread`, it is unlocked before
// returning. The caller continues on a different thread.
func RunOnOSThread(f func()) The advantages are:
The disadvantages are:
// RunOnOSThread panics if run from an init function. at the cost of a panic condition and forcing main thread APIs to require some call from the main goroutine. E.g. package app // gioui.org/app
// Window represent a platform GUI window.
// Because of platform limitations, at least once one of Window's
// methods must be called from the main goroutine.
// Otherwise, call [Main].
type Window struct {...}
// Main must be called from the main goroutine at least once,
// if no [Window] methods will be called from the main goroutine.
func Main() |
Gentle ping. I believe #64777 (comment) addresses the objections @aclements brought up here and on #64755. |
As I understand it the goal here is for an imported package to be able to run non-Go code on the initial program thread. The Honestly it does not seem so terrible to me if an operation that has to take place on the initial program thread requires some cooperation from the |
The number of direct uses of The alternative is not entirely trivial either. Consider a straightforward CLI tool that you want to add an optional GUI to, or a CLI service you optionally want to run as a Windows service. You then have to change your program flow, e.g. rewriting your logic as an interface with callbacks, and you must call the API from your main function. Both requirements are alien to Go programmers not familiar with the underlying frameworks. Case in point, it took me a while to figure out why
So let's not pass on the annoyance to Go programmers :-) Go often makes quality-of-life changes that are not strictly necessary, sometimes at non-trivial cost: |
Proposal Details
This proposal is a less flexible but perhaps easier to implement and maintain alternative to #64755. See that issue for background and motivation for non-main packages to take control of the main thread.
I propose adding a new function,
runtime.RunOnMainThread
, for running a function on the startup, or main, thread. In particular,This is the complete proposal.
Variants
Just like #64755, an alternative spelling is
syscall.RunOnMainThread
optionally limited toGOOS=darwin
andGOOS=ios
.The text was updated successfully, but these errors were encountered: