TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Structured concurrency and Lua

125 pointsby marcobambiniover 2 years ago

6 comments

vanderZwanover 2 years ago
&gt; <i>It achieves this in ways that seem subtle to us-- clearly so, since it wasn&#x27;t really discovered and applied until the late 2010&#x27;s</i><p>I&#x27;m getting a little annoyed by the structured concurrency crowd ignoring the synchronous concurrency paradigm as a historical precedent. It has existed since the 1980s and as far as I can tell there is a huge overlap in concepts. The only major difference I&#x27;m seeing is the addition of scoped <i>asynchronous</i> concurrency on top of synchronous concurrency (meaning single-threaded concurrency). That&#x27;s significant for sure, but also not so much that it&#x27;s cool to ignore the existing previous work.<p>Here&#x27;s Céu, a language from 2011 with scoped synchronous concurrency:<p><pre><code> input int KEY; par&#x2F;or do every 1s do _printf(&quot;Hello World!\n&quot;); end with await KEY; end </code></pre> (Prints the “Hello World!” message every second, terminating on a key press.)<p>And hey, since we&#x27;re talking about Lua, maybe we can acknowledge LuaGravity, which has existed since 2009?<p>Don&#x27;t get me wrong though, I&#x27;m glad structured concurrency is catching on and that new work is being done in this area.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Synchronous_programming_language" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Synchronous_programming_langua...</a><p>[1] <a href="http:&#x2F;&#x2F;ceu-lang.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;ceu-lang.org&#x2F;</a><p>[2] <a href="https:&#x2F;&#x2F;fsantanna.github.io&#x2F;luagravity&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fsantanna.github.io&#x2F;luagravity&#x2F;</a>
评论 #32913138 未加载
评论 #32959949 未加载
评论 #32916161 未加载
评论 #32918407 未加载
评论 #32917270 未加载
ufoover 2 years ago
This is taking advantage of coroutines and to-be-closed variables.<p>To-be-closed variables were recently introduced in Lua 5.4. Lua runs a destructor method when these variables go out of scope, like the RAII pattern in c++.<p>Coroutines have been in Lua for quite a while now. They allow cooperative concurrency (as opposed to preemptive concurrency a-la threads). It works similar to async generators as found in Python, C#, etc, except that Lua coroutines are stackful. You can yield inside an inner function and it&#x27;ll pause the whole call stack. In the article, the yield is inside the async_sleep function. When you call a function that yields, you don&#x27;t need to explicitly mark that with an &quot;await&quot;.
评论 #32912739 未加载
andrewmcwattersover 2 years ago
I find concurrency (and threading) concepts in Lua fun and all, but these topics seem to ignore practical issues for academic, historical, or theoretical ones.<p>I use Lua a lot, but the global interpreter lock or lua_lock&#x2F;lua_unlock implementation detail is one I don&#x27;t see people talk too much about.<p>It&#x27;s particularly frustrating when you want to do something advanced, like pull in an FFI for Lua, but also implement GIL behavior.<p>I haven&#x27;t ever seen someone implement this correctly. I think it should be easy in theory, but I haven&#x27;t given it a go in a while.<p>The current state of things is, you can use LuaJIT and accept no thread safety, or use PUC-Rio Lua with a custom lua_open&#x2F;lua_lock implementation and use the FFI library separately. I haven&#x27;t actually seen anyone do the latter. So in the field, it&#x27;s all an untested concept, I think.<p>Maybe someone out there is doing this today and not talking about it. But there&#x27;s no open source solution. Lua Lanes doesn&#x27;t address this concern, and stylistic adaptions of cooperative multitasking don&#x27;t either.<p>I realize it&#x27;s technically a different topic, but it&#x27;s basically the same area of concern.<p>Except, well, I find there to be fewer practical use cases for cooperative multitasking versus preemptive.
评论 #32919484 未加载
belmarcaover 2 years ago
Does HN have a solid book recommendation on the topic of concurrency?
评论 #32911472 未加载
评论 #32911437 未加载
_ZeD_over 2 years ago
The equivalent for python is trio<p><a href="https:&#x2F;&#x2F;trio.readthedocs.io&#x2F;en&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;trio.readthedocs.io&#x2F;en&#x2F;stable&#x2F;</a>
aurahamover 2 years ago
I skimmed the code example and although I understand that it spawn two tasks, the syntax is not so clear for me. I rather prefer the actor-based model of concurrency of Elixir&#x2F;Erlang instead of async functions and Go channels. I somewhat biased, though.