
Go style Channels for Lua - luu
https://github.com/majek/lua-channels
======
majke
Author here. Here's some context [1]:

 _Lua supports cooperative coroutines out of the box, which is great.
Unfortunately Lua lacks built-in communication primitives. To fill the void I
've implemented Go style channels in pure Lua.

Of course abstract channels without any concurrent IO aren't terribly useful,
but fortunately the code is pretty straightforward and adapting it to your
event loop shouldn't be a problem._

The code is ~400 lines of code + ~200 of tests.

For example, here's [2] Eratosthenes sieve following Russ Cox explanation [3].

[1] [http://lua-users.org/lists/lua-l/2013-08/msg00546.html](http://lua-
users.org/lists/lua-l/2013-08/msg00546.html)

[2] [https://github.com/majek/lua-
channels/blob/master/sieve.lua](https://github.com/majek/lua-
channels/blob/master/sieve.lua)

[3] [http://swtch.com/~rsc/thread/](http://swtch.com/~rsc/thread/)

~~~
corysama
I'm not familiar enough with Go's channels to understand what I'm looking at.
Could you give a brief summary of the functionality this adds over
passing/returning values through Lua's built-in resume/yield?

~~~
majke
I'll do my best, but it's a bit hard to explain.

Basically channels are yet-another synchronization paradigm like locking or
shared memory, some people like it some don't. Channels are an implementation
of Hoare's CSP [0].

Channels can be passed around, the same could be said about Lua coroutines.
But with channels you get more flexibility:

\- you can "block" on many channels at once using the "chanalt" statement. You
can "resume" only one lua coroutine at a time.

\- channels they can buffer messages (ie: channels can queue several values)

\- many coroutines may "send" a value to a channel at the same time, many
coroutines may try to "listen" (receive) a value at the same time.

\- finally you can pass a channel over a channel

I find channels, as opposed to many other synchronization paradigms, to be
composable and generally nice in use.

It's a "better" alternative to synchronization locking and mutable data. But
in Lua, of course, there aren't multiple threads so why synchronization is
useful? In the same way that coroutines are useful - they allow you to
decompose a program into set of smaller programs. The example given by Russ
Cox is the Eratosthenes sieve. You can read about it [1] or watch him talking
about it [2], finally my implementation following his talk [3].

[0]
[https://en.wikipedia.org/wiki/Communicating_sequential_proce...](https://en.wikipedia.org/wiki/Communicating_sequential_processes)

[1] [http://swtch.com/~rsc/thread/](http://swtch.com/~rsc/thread/)

[2]
[http://www.youtube.com/watch?feature=player_detailpage&v=hB0...](http://www.youtube.com/watch?feature=player_detailpage&v=hB05UFqOtFA#t=1600s)

[3] [https://github.com/majek/lua-
channels/blob/master/sieve.lua](https://github.com/majek/lua-
channels/blob/master/sieve.lua)

------
rob05c
His implementation uses libtask, which says it schedules coroutines with "a
single kernel thread." Looking at his code, he doesn't appear to extend this.

Go provides microthreads, which are both concurrent and parallel, and
extremely lightweight.

Libtask provides concurrency. It does not provide parallelism. Parallelism is
important.

~~~
majke
_His implementation uses libtask_

No. I just implement libtask/channel.c in Lua.

 _It does not provide parallelism. Parallelism is important._

I'm doing it in pure Lua. Lua doesn't use multiple OS threads. Doing channels
_across_ Lua contexts would be an interesting thing.

~~~
rob05c
Ah, I misunderstood your project page.

 _Lua doesn 't use multiple OS threads._

I didn't know that. That's really disappointing. Parallelism really is
important. It sounds like it's possible, just really awkward:
[http://kotisivu.dnainternet.net/askok/bin/lanes/](http://kotisivu.dnainternet.net/askok/bin/lanes/)

Kudos on making a nice CSP library for Lua though. In my opinion, CSP is a
good paradigm for high-level concurrent and parallel processing. Hopefully one
day Lua will add reasonable parallelism.

------
knodi
If i embed Lua in my go app via C bindings. Would it be possible to pass
messages via lua channels to go channel and have it send/receive messages on
that channel.

Could this be possible?

sorry I know this is out side the scope of this project.

------
dubcanada
Wait did I miss something, aren't Go Channels just coroutines and doesn't Lua
already have one of the best if not the best coroutines implementation that
exists in a "scripting language".

Maybe it's just a wrapper? I don't understand the reasoning for this.

~~~
masklinn
Go channels are bounded queues[0], Go's goroutines are coroutines.

[0] with a twist, the default size is 0 meaning unbuffered: items can't be
_stored_ in the queue, so a PUT will block until a GET is performed

------
mamcx
Any similar implementation but for obj-c?

