lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sam Roberts wrote:
[...]
> - erlang allows data structures (+code/objects, I think) to be
>   transparently serialized into messages.  So, even though it enforces
>   separation of states, it also allows easy transport of data between
>   states. The lua implementation I've seen of this don't involve sending
>   code, and usually allow only a few primitive types to be marshalled.

This is CSP, the 'other' way of writing multithreaded applications --- most
systems call it message passing, but that's really overly simplistic. CSP is
more a way of life than an algorithm... it's thought highly of in academic
circles because it's much easier to reason about than shared memory
concurrency; it's simpler to prove CSP algorithms correct. Another well-known
language that's based around CSP is Occam, which practically invented concurrency.

CSP typically has two primitives: send() and recv(). In Lua terms, they'd work
like this:

function adderthread(in, out)
	while true do
		local i = recv(in)
		send(out, i+1)
	end
end

function main()
	local slavein, slaveout = CreateThread(adderthread)
	for i = 1, 10 do
		send(slaveout, i)
		local j = recv(slavein, i)
		print(i.."+1 = "..j)
	end
end

Other typical features include type checking (so that if you send an int where
the thing on the other end wanted a pixmap, you get a clean exception rather
than a crash), variadic messages (so that the thing on other end can detect
whether you sent an int or a pixmap and do the right thing), parallel recv()s
(so you can monitor several input channels simultaneously), buffered sends (so
that you can send() without waiting for the other end to respond)...

Pure CSP forbids threads to touch objects belonging to another thread. This
has obvious disadvantages, but also some advantages: it means that you can run
threads in different address spaces. Or different machines. It lends itself
beautifully to clustering.

(There's also other cool stuff you can do with CSP, such as Linda.)

You could do CSP on Lua with fork(), pipe(), and a fast marshalling algorithm.
Not a lot of code.

[...]
> Lua is scheme-minimalist in many ways, but one thing the lisps have
> usually provided (always?) is data marshalling. You can write and
> read recursive data structures natively. I know, there are dozens of
> implementations of this out there for lua... but maybe thats a bad
> thing.

Fast marshalling is dead handy. I'd even argue that it was so handy that it
belonged in the core language...

-- 
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out how to
│ use my telephone." --- Bjarne Stroustrup

Attachment: signature.asc
Description: OpenPGP digital signature