{"id":15893958,"url":"https://github.com/chinanf-boy/book-green-threads-explained-zh","last_synced_at":"2025-04-02T18:16:43.153Z","repository":{"id":90548068,"uuid":"194617279","full_name":"chinanf-boy/book-green-threads-explained-zh","owner":"chinanf-boy","description":"中文翻译:\u003ccfsamson/book-green-threads-explained\u003e 绿色线程是个啥教程(本repo是gitbook自动链接) :heart: 校对 中","archived":false,"fork":false,"pushed_at":"2019-07-02T02:08:09.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T08:47:17.723Z","etag":null,"topics":["goruntime","greenthread","rust","zh"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chinanf-boy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-01T06:59:15.000Z","updated_at":"2019-07-02T02:08:11.000Z","dependencies_parsed_at":"2023-07-23T22:46:39.747Z","dependency_job_id":null,"html_url":"https://github.com/chinanf-boy/book-green-threads-explained-zh","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fbook-green-threads-explained-zh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fbook-green-threads-explained-zh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fbook-green-threads-explained-zh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fbook-green-threads-explained-zh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chinanf-boy","download_url":"https://codeload.github.com/chinanf-boy/book-green-threads-explained-zh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246866100,"owners_count":20846496,"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":["goruntime","greenthread","rust","zh"],"created_at":"2024-10-06T08:14:09.054Z","updated_at":"2025-04-02T18:16:43.130Z","avatar_url":"https://github.com/chinanf-boy.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ndescription: '\u003e-本书的目的就是解释什么是绿色线程。 我们会用一个代码实现示例，虽小但能工作 —— 用实现的绿色线程去执行代码。'\n---\n\n# 绿色线程是个啥教程\n\n## cfsamson/book-green-threads-explained  [![](http://llever.com/translate.svg)](https://github.com/chinanf-boy/chinese-translate-list)\n\n「 绿色线程是个啥教程 」\n\n[中文](./readme.zh.md) \\| [english](https://github.com/cfsamson/book-green-threads-explained)\n\n### 校对 🀄️\n\n| 翻译的原文 | 与日期 | 最新更新 | 更多 |\n| :--- | :--- | :--- | :--- |\n| [commit](https://github.com/cfsamson/book-green-threads-explained/tree/4599d94cf2b9ad3ed5cad45134e67b3a63ccf129) | ⏰ 2019-06-26 | ![](https://img.shields.io/github/last-commit/cfsamson/book-green-threads-explained.svg) | [中文翻译](https://github.com/chinanf-boy/chinese-translate-list) |\n\n* [x] README.md\n* [ ] [green-threads.md](src/green-threads.zh.md)\n* [ ] [background-information.md](src/background-information.zh.md)\n* [ ] [an-example-we-can-build-upon.md](src/an-example-we-can-build-upon.zh.md)\n* [ ] [the-stack.md](src/the-stack.zh.md)\n* [ ] [simple-but-unsafe-green-thread-implementation.md](src/simple-but-unsafe-green-thread-implementation.zh.md)\n* [ ] [final-200-lines-of-code.md](src/final-200-lines-of-code.zh.md)\n* [ ] [supporting-windows.md](src/supporting-windows.zh.md)\n\n#### 贡献\n\n欢迎 👏 勘误/校对/更新贡献 😊 [具体贡献请看](https://github.com/chinanf-boy/chinese-translate-list#贡献)\n\n### 生活\n\n[If help, **buy** me coffee —— 营养跟不上了，给我来瓶营养快线吧! 💰](https://github.com/chinanf-boy/live-need-money)\n\n## 介绍\n\n{% hint style=\"info\" %}\n`我们在这里用到的所有代码都[在 github 存储库](https://github.com/cfsamson/example-greenthreads)。它会有两个分支，`main`只包含代码，和`commented`分支的带有注释的代码，就是解释我们所做工作的注释。`\n{% endhint %}\n\n绿色线程，用户线程，goroutine\\(go 协程\\) 或纤维\\(fibers\\)，它们有许多名称，但为了简单起见，我将从现在开始，把它们统称为绿色线程。\n\n在本文中，我想通过实现一个非常简单的示例来探索它们是如何工作的，我们用 200 行 Rust 代码，创建自己的绿色线程。我们将在整个过程中解释所有内容，因此我们主要关注的是，通过使用简单但有效的示例来理解它们，并了解它们的工作原理。\n\n{% hint style=\"info\" %}\n`我们不会使用任何外部库或帮助程序，并将从头开始做所有事情，因此我们可以确保我们真正了解，发生的事情。`\n{% endhint %}\n\n### 这篇文章的目标群众？\n\n这篇文章中，我们正在偷看兔子洞\\(我们不知道洞里面，还有什么在等着我们\\)，所以如果这听起来很可怕，那么这篇文章可能不适合你。回去吧，过得幸福。\n\n如果你是一个好奇的人，想要了解事情的运作方式，请继续阅读。也许你已经听说过 Go 及其 goroutines，或 Ruby 或 Julia 中的等价物，你知道如何使用它们，但想知道它们是如何工作的 - 那么请继续阅读。\n\n此外，如果：\n\n* 您是 Rust 的新手，想要了解有关其功能（features）的更多信息。\n* 您已经参与了 Rust 社区中，有关 async / await，Pin-API 以及我们需要生成器的原因的讨论。在这种情况下，我尝试将所有部分中和在一起。\n* 如果您想学习 Rust 中，内联\\(inline\\)汇编的基础知识。\n* 如果你只是好奇。\n\n好的，请与我一起，让我们试图弄清楚需要了解它们的一切。\n\n您不需要是 Rust 程序员，才能理解本文，但强烈建议您首先阅读一些基本语法。如果你想长时间跟踪或克隆存储库并使用代码，你应该得到 Rust 并学习到了基础知识。\n\n{% hint style=\"info\" %}\n`[装载 Rust 宝藏，都放在了这里](https://www.rust-lang.org/tools/install)`\n{% endhint %}\n\n### 跟上\n\n我在这里提供的所有代码都在一个文件中，并且没有依赖关系，这意味着您可以轻松地启动自己的项目，并按照您的要求进行操作（我建议你这样做）。你甚至可以在[Rust playground](https://play.rust-lang.org)运行大部分代码。只要记得编译器要为`nightly`版本。\n\n### 便携性和问题\n\n目前我遇到了一个问题：`asm!`宏在发布模式下无法编译。它似乎与我在内联宏中使用的`\"=m\"`约束有关。\n\n**编辑 2019-06-21：** 我决定解决这个问题，并更改内联汇编，以在发布版本上编译和运行。\n\n我已经在 OSX，Linux 和 Windows 上测试了代码。\n\n### 免责声明 \u003ca id=\"docs-internal-guid-12e6c217-7fff-3de7-4bee-4532b47ef574\"\u003e\u003c/a\u003e\n\n我不打算在这里做一个完美的实现。我正在偷偷摸摸地深入了解本质，并想把这些内容，'铸'成文章，但现在扩展成了一本小书。其实这不是展示 Rust 最强优势 —— 它的安全保证的最佳方式，但它确实显示了 Rust 的一个有趣的用法，代码大多非常干净，易于跟踪。\n\n但是，如果您发现我可以使代码更安全，而不会使其显着更复杂的地方，我欢迎您在[the repo](https://github.com/cfsamson/example-greenthreads)创建一个问题，甚至最好是一个拉动请求。\n\n### 赞誉\n\n[Quentin Carbonneaux](https://github.com/mpu)早在 2013 年，写了一篇[好的博文](https://c9x.me/articles/gthreads/intro.html)，作为我主要代码示例的灵感。谢谢[nickelpro](https://github.com/nickelpro)有关 Windows 支持的帮助和反馈。\n\n### 编辑\n\n2019-06-18：实现适当 Windows 支持的新章节\n\n2019-06-21：相当大的改变和清理。据反馈，Valgrind 报告了一些代码问题并且崩溃了。现在已经修复，目前还没有未解决的问题。此外，代码现在，`debug`和`release`（模式）可在所有平台上构建与运行，都没有任何问题。感谢大家报告他们发现的问题。\n\n2019-06-26： Windows 支持附录，把`XMM`字段视为 64 位，但它们是 128 位，这是我的疏忽。纠正这一点为该章增加了一些有趣的材料，但不好的一点是，也带来了一些复杂性。但是，它现在已有了纠正和说明。\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinanf-boy%2Fbook-green-threads-explained-zh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchinanf-boy%2Fbook-green-threads-explained-zh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinanf-boy%2Fbook-green-threads-explained-zh/lists"}