{"id":19225596,"url":"https://github.com/golang-design/mainthread","last_synced_at":"2025-04-21T00:32:17.239Z","repository":{"id":43279428,"uuid":"306420013","full_name":"golang-design/mainthread","owner":"golang-design","description":"🔀 schedule functions on the main thread","archived":false,"fork":false,"pushed_at":"2022-03-10T09:11:01.000Z","size":37,"stargazers_count":36,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-01T07:21:55.399Z","etag":null,"topics":["go","golang","mainthread"],"latest_commit_sha":null,"homepage":"https://golang.design/s/mainthread","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/golang-design.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-22T18:02:51.000Z","updated_at":"2025-03-22T22:37:24.000Z","dependencies_parsed_at":"2022-09-04T19:02:05.480Z","dependency_job_id":null,"html_url":"https://github.com/golang-design/mainthread","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-design%2Fmainthread","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-design%2Fmainthread/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-design%2Fmainthread/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-design%2Fmainthread/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/golang-design","download_url":"https://codeload.github.com/golang-design/mainthread/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249980428,"owners_count":21355425,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","mainthread"],"created_at":"2024-11-09T15:15:44.978Z","updated_at":"2025-04-21T00:32:16.963Z","avatar_url":"https://github.com/golang-design.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mainthread [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/mainthread)](https://pkg.go.dev/golang.design/x/mainthread) ![mainthread](https://github.com/golang-design/mainthread/workflows/mainthread/badge.svg?branch=main) ![](https://changkun.de/urlstat?mode=github\u0026repo=golang-design/mainthread)\n\nschedule functions to run on the main thread\n\n```go\nimport \"golang.design/x/mainthread\"\n```\n\n## Features\n\n- Main thread scheduling\n- Schedule functions without memory allocation\n\n## API Usage\n\nPackage mainthread offers facilities to schedule functions\non the main thread. To use this package properly, one must\ncall `mainthread.Init` from the main package. For example:\n\n```go\npackage main\n\nimport \"golang.design/x/mainthread\"\n\nfunc main() { mainthread.Init(fn) }\n\n// fn is the actual main function\nfunc fn() {\n\t// ... do stuff ...\n\n\t// mainthread.Call returns when f1 returns. Note that if f1 blocks\n\t// it will also block the execution of any subsequent calls on the\n\t// main thread.\n\tmainthread.Call(f1)\n\n\t// ... do stuff ...\n\n\n\t// mainthread.Go returns immediately and f2 is scheduled to be\n\t// executed in the future.\n\tmainthread.Go(f2)\n\n\t// ... do stuff ...\n}\n\nfunc f1() { ... }\nfunc f2() { ... }\n```\n\nIf the given function triggers a panic, and called via `mainthread.Call`,\nthen the panic will be propagated to the same goroutine. One can capture\nthat panic, when possible:\n\n```go\ndefer func() {\n\tif r := recover(); r != nil {\n\t\tprintln(r)\n\t}\n}()\n\nmainthread.Call(func() { ... }) // if panic\n```\n\nIf the given function triggers a panic, and called via `mainthread.Go`,\nthen the panic will be cached internally, until a call to the `Error()` method:\n\n```go\nmainthread.Go(func() { ... }) // if panics\n\n// ... do stuff ...\n\nif err := mainthread.Error(); err != nil { // can be captured here.\n\tprintln(err)\n}\n```\n\nNote that a panic happens before `mainthread.Error()` returning the\npanicked error. If one needs to guarantee `mainthread.Error()` indeed\ncaptured the panic, a dummy function can be used as synchornization:\n\n```go\nmainthread.Go(func() { panic(\"die\") })\t// if panics\nmainthread.Call(func() {}) \t\t\t\t// for execution synchronization\nerr := mainthread.Error()\t\t\t\t// err must be non-nil\n```\n\n\nIt is possible to cache up to a maximum of 42 panicked errors.\nMore errors are ignored.\n\n## When do you need this package?\n\nRead this to learn more about the design purpose of this package:\nhttps://golang.design/research/zero-alloc-call-sched/\n\n## Who is using this package?\n\nThe initial purpose of building this package is to support writing\ngraphical applications in Go. To know projects that are using this\npackage, check our [wiki](https://github.com/golang-design/mainthread/wiki)\npage.\n\n\n## License\n\nMIT | \u0026copy; 2021 The golang.design Initiative Authors, written by [Changkun Ou](https://changkun.de).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolang-design%2Fmainthread","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgolang-design%2Fmainthread","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolang-design%2Fmainthread/lists"}