编程与软件工程chiark.greenend.org.uk编辑精选
C 语言中的协程(2000)
Coroutines in C (2000)
2010 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
Simon Tatham 这篇 2000 年的经典文章展示了如何用 C 语言的 switch/case 和宏等技巧实现协程,让函数可以保存状态、暂停并在下次调用时从断点恢复。文章还讨论了这种写法的局限(如不可重入、与 switch 语句的冲突),是 C 语言元编程的著名范例。
为什么经久不衰:二十多年过去,它仍是理解协程本质和 C 语言技巧的必读文章,2010、2019、2024 三次登上 HN 首页足见其生命力。
HN 评论精选
推荐 libco 库,并分享在生产项目中用协程处理异步 IO 的经验
我们大量使用的一个很不错的库是 libco,它支持多种架构和实现机制。我们在 Fluent Bit 中广泛使用它,用 epoll + 协程来管理异步 IO 网络操作。
edsiper2查看英文原文
A really good library that we use is libco[0], it supports different architectures and mechanisms. We use it extensively on Fluent Bit[1] to manage async IO network operations (epoll + coroutines). - [0] https://byuu.org/library/libco/ - [1] https://fluentbit.io
推荐 libdill/libmill 两个风格不同的 C 协程库
有个很酷的 C 协程库,既有符合 C 惯用写法的版本(libdill),也有照搬 Go 风格逐行翻译的版本(libmill)。
brobdingnagians查看英文原文
A cool library that implements C coroutines, available for both idiomatic C (http://libdill.org/) and in the same style as Go (http://libmill.org/) with line by line translations
基于该思路实现了包装 libev 的异步 C 编程概念验证
我去年顺着这个思路写了一个概念验证库,用约 250 行宏包装 libev,可以内联编写异步 C 代码。代码看起来相当怪异,但最终能用,还附了一个简单的 echo server 示例。
jeremycw查看英文原文
I ran with this idea last year and wrote a proof of concept library that wraps libev using using this concept to allow you to write async C code inline. It's about 250 lines of macro abuse and the resulting code looks pretty funky but it ultimately works. I have a simple echo server example here: https://github.com/jeremycw/zasync/blob/master/echo_server.c
认为这种宏技巧可读性差,显式传递状态的方式更清晰
作为 OOP 开发者,这种写法让我有点害怕,虽然它能用(但肯定不是线程安全的吧?)。即便在 C 里,我觉得把函数写成纯函数、返回 (c, state) 元组并把状态传回调用者,概念上更简单。用这种非惯用的技巧和宏更难读懂。
plaguuuuuu查看英文原文
As an OOP dev this kind of horrifies me, even though it works okay (though it's surely not thread-safe?) Nevertheless, even in C, I'd find it conceptually simpler to understand if the functions were written as pure functions, returning a tuple with (c, state) and passing the state back into the caller. Using non-idiomatic tricks and macros in this way is harder to read and understand.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选