mainthread

package module
v0.0.0-...-8b78f0a Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2017 License: MIT Imports: 2 Imported by: 187

README

mainthread GoDoc Report card

Package mainthread allows you to run code on the main operating system thread.

go get github.com/faiface/mainthread

Operating systems often require, that code which deals with windows and graphics has to run on the main thread. This is however somehow challenging in Go due to Go's concurrent nature.

This package makes it easily possible.

All you need to do is put your main code into a separate function and call mainthread.Run from your real main, like this:

package main

import (
	"fmt"

	"github.com/faiface/mainthread"
)

func run() {
	// now we can run stuff on the main thread like this
	mainthread.Call(func() {
		fmt.Println("printing from the main thread")
	})
	fmt.Println("printing from another thread")
}

func main() {
	mainthread.Run(run) // enables mainthread package and runs run in a separate goroutine
}

More functions

If you don't wish to wait until a function finishes running on the main thread, use mainthread.CallNonBlock:

mainthread.CallNonBlock(func() {
	fmt.Println("i'm in the main thread")
})
fmt.Println("but imma be likely printed first, cuz i don't wait")

If you want to get some value returned from the main thread, you can use mainthread.CallErr or mainthread.CallVal:

err := mainthread.CallErr(func() error {
	return nil // i don't do nothing wrong
})
val := mainthread.CallVal(func() interface{} {
	return 42 // the meaning of life, universe and everything
})

If mainthread.CallErr or mainthread.CallVal aren't sufficient for you, you can just assign variables from within the main thread:

var x, y int
mainthread.Call(func() {
	x, y = 1, 2
})

However, be careful with mainthread.CallNonBlock when dealing with local variables.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var CallQueueCap = 16

CallQueueCap is the capacity of the call queue. This means how many calls to CallNonBlock will not block until some call finishes.

The default value is 16 and should be good for 99% usecases.

Functions

func Call

func Call(f func())

Call queues function f on the main thread and blocks until the function f finishes.

func CallErr

func CallErr(f func() error) error

CallErr queues function f on the main thread and returns an error returned by f.

func CallNonBlock

func CallNonBlock(f func())

CallNonBlock queues function f on the main thread and returns immediately. Does not wait until f finishes.

func CallVal

func CallVal(f func() interface{}) interface{}

CallVal queues function f on the main thread and returns a value returned by f.

func Run

func Run(run func())

Run enables mainthread package functionality. To use mainthread package, put your main function code into the run function (the argument to Run) and simply call Run from the real main function.

Run returns when run (argument) function finishes.

Example
package main

import (
	"fmt"

	"github.com/faiface/mainthread"
)

func run() {
	mainthread.Call(func() {
		fmt.Println("i'm printing from the main thread")
	})
}

func main() {
	mainthread.Run(run)
}
Output:

i'm printing from the main thread

Types

This section is empty.

Jump to

Keyboard shortcuts

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