mainthread

package
v0.1.0-alpha.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mainthread pins the Go program's main goroutine to the process main thread and lets the rest of the program run on goroutines while the OS UI loop owns that thread. Cocoa requires this (NSApplication and all windows must live on the main thread); on Linux and Windows the shells run their own loops on private threads and Run merely provides the same program shape everywhere:

func main() {
    mainthread.Run(func() {        // app code runs on a goroutine
        win, _ := auto.Open(cfg)
        ...
    })                             // returns when the func returns
}

Index

Constants

This section is empty.

Variables

View Source
var ErrNotRunning = errors.New("mainthread: Run is not active (call mainthread.Run from main)")

ErrNotRunning is returned by Do/Post when Run is not active.

Functions

func Do

func Do(f func()) error

Do runs f on the main thread and waits for it to finish. A panic in f is re-raised in the caller. Do must not be called from the main thread itself (i.e. from inside another Do/Post callback): it would deadlock.

func Post

func Post(f func()) error

Post runs f on the main thread without waiting for it.

func Run

func Run(app func())

Run executes app on a new goroutine and runs the platform loop on the calling (main) thread until app returns. It must be called from main().

func Running

func Running() bool

Running reports whether Run is active.

func SetPlatform

func SetPlatform(p Platform)

SetPlatform replaces the default channel-based loop. Call from init().

Types

type Platform

type Platform interface {
	Run()
	Post(f func())
	Stop()
}

Platform is the OS-specific main-thread loop (registered by the cocoa package). Run blocks on the main thread until Stop is called. Post queues f to run on the main thread and may be called from any goroutine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL