What's the difference between a "coroutine" and a "thread" in Lua?

classic Classic list List threaded Threaded
47 messages Options
123
Reply | Threaded
Open this post in threaded view
|

What's the difference between a "coroutine" and a "thread" in Lua?

John Klimek
I'm a little confused because the terms "coroutine" and "thread" are
used somewhat interchangeably in the Lua documentation.

Are Lua "coroutines" and "threads" the same exact thing?

Thanks...

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Aaron Brown
John Klimek wrote:

Are Lua "coroutines" and "threads" the same exact thing?

Yes:

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
print(type(coroutine.create(function() end)))
thread

The non-Lua type of thread can be referred to as an "OS
thread".

--
Aaron
http://arundelo.com/


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Asko Kauppi
In reply to this post by John Klimek

Suggestion to the Lua authors:

In next version, you might consider using "coroutine" everywhere where thread now is (including the type name!). This is because multithreading packets will be more and more used, like LuaSocket already is for net stuff, and the "thread" reference does confuse people.

With multithreading packages, there will be "threads" (the threads the packets provide, Lua constructs) and "OS threads" (the underlying threads, which might or might not have a 1:1 mapping to Lua threads). And OS threads map to kernel and user threads.... :)

-asko


John Klimek kirjoitti 10.8.2007 kello 6:15:

I'm a little confused because the terms "coroutine" and "thread" are
used somewhat interchangeably in the Lua documentation.

Are Lua "coroutines" and "threads" the same exact thing?

Thanks...


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Duncan Cross
In reply to this post by John Klimek
On 8/10/07, John Klimek <[hidden email]> wrote:
> I'm a little confused because the terms "coroutine" and "thread" are
> used somewhat interchangeably in the Lua documentation.
>
> Are Lua "coroutines" and "threads" the same exact thing?
>
> Thanks...
>

No. Threads are the underlying thing that coroutines are implemented
on top of. Coroutines are threads created in a particular way with a
specific target function, that can be resumed and yielded, but not all
threads are set up this way - if you create a thread via the C api
that has nothing on its stack, that is not (yet) a coroutine.

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Stefan Sandberg
In reply to this post by Asko Kauppi
I second this!

Asko Kauppi wrote:

Suggestion to the Lua authors:

In next version, you might consider using "coroutine" everywhere where thread now is (including the type name!). This is because multithreading packets will be more and more used, like LuaSocket already is for net stuff, and the "thread" reference does confuse people.

With multithreading packages, there will be "threads" (the threads the packets provide, Lua constructs) and "OS threads" (the underlying threads, which might or might not have a 1:1 mapping to Lua threads). And OS threads map to kernel and user threads.... :)

-asko


John Klimek kirjoitti 10.8.2007 kello 6:15:

I'm a little confused because the terms "coroutine" and "thread" are
used somewhat interchangeably in the Lua documentation.

Are Lua "coroutines" and "threads" the same exact thing?

Thanks...




Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Gregg Reynolds-2
In reply to this post by Asko Kauppi
On 8/10/07, Asko Kauppi <[hidden email]> wrote:
>
> Suggestion to the Lua authors:
>
> In next version, you might consider using "coroutine" everywhere
> where thread now is (including the type name!). This is because
..
> With multithreading packages, there will be "threads" (the threads
> the packets provide, Lua constructs) and "OS threads" (the underlying
> threads, which might or might not have a 1:1 mapping to Lua threads).
> And OS threads map to kernel and user threads.... :)

I strongly second.  From the manual:

"The type thread represents independent threads of execution and it is
used to implement coroutines (see §2.11). Do not confuse Lua threads
with operating-system threads. Lua supports coroutines on all systems,
even those that do not support threads."

"A coroutine in Lua represents an independent thread of execution.
Unlike threads in multithread systems, however, a coroutine only
suspends its execution by explicitly calling a yield function."

Q: when is a thread not a thread?  A:  (...your witticism here...)

For me at least this language is pretty confusing.  However, I can see
the wisdom of distinguishing between coroutine (which is perforce a
kind of routine, function, or the like) and a thread (qua machine
state, execution context, whatever).  I suggest renaming type thread;
the wikipedia article on thread suggests a useful term:

"In this article, the term "thread" is used to refer to kernel
threads, whereas "fiber" is used to refer to user threads. Fibers are
co-operatively scheduled: a running fiber must explicitly "yield" to
allow another fiber to run. A fiber can be scheduled to run in any
thread in the same process...Many fiber implementations are entirely
in userspace. As a result, context switching between fibers within the
same process is extremely efficient because it does not require any
interaction with the kernel at all: a context switch can be performed
by locally saving the CPU registers used by the currently executing
fiber and then loading the registers required by the fiber to be
executed. Since scheduling occurs in userspace, the scheduling policy
can be more easily tailored to the requirements of the program's
workload."

Does the lua type thread fit this description?  If so I would heartily
recommend using "fiber".

-gregg


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Ana Lucia Moura
I heartily recommend using "coroutine", the proper denomination for the concept, which was introduced more than 40 years ago :)

Ana

2007/8/10, Gregg Reynolds <[hidden email]>:
On 8/10/07, Asko Kauppi <[hidden email]> wrote:
>
> Suggestion to the Lua authors:
>
> In next version, you might consider using "coroutine" everywhere
> where thread now is (including the type name!). This is because
..
> With multithreading packages, there will be "threads" (the threads
> the packets provide, Lua constructs) and "OS threads" (the underlying
> threads, which might or might not have a 1:1 mapping to Lua threads).
> And OS threads map to kernel and user threads.... :)

I strongly second.  From the manual:

"The type thread represents independent threads of execution and it is
used to implement coroutines (see §2.11). Do not confuse Lua threads
with operating-system threads. Lua supports coroutines on all systems,
even those that do not support threads."

"A coroutine in Lua represents an independent thread of execution.
Unlike threads in multithread systems, however, a coroutine only
suspends its execution by explicitly calling a yield function."

Q: when is a thread not a thread?  A:  (...your witticism here...)

For me at least this language is pretty confusing.  However, I can see
the wisdom of distinguishing between coroutine (which is perforce a
kind of routine, function, or the like) and a thread (qua machine
state, execution context, whatever).  I suggest renaming type thread;
the wikipedia article on thread suggests a useful term:

"In this article, the term "thread" is used to refer to kernel
threads, whereas "fiber" is used to refer to user threads. Fibers are
co-operatively scheduled: a running fiber must explicitly "yield" to
allow another fiber to run. A fiber can be scheduled to run in any
thread in the same process...Many fiber implementations are entirely
in userspace. As a result, context switching between fibers within the
same process is extremely efficient because it does not require any
interaction with the kernel at all: a context switch can be performed
by locally saving the CPU registers used by the currently executing
fiber and then loading the registers required by the fiber to be
executed. Since scheduling occurs in userspace, the scheduling policy
can be more easily tailored to the requirements of the program's
workload."

Does the lua type thread fit this description?  If so I would heartily
recommend using "fiber".

-gregg

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Gregg Reynolds-2
On 8/10/07, Ana Lucia Moura <[hidden email]> wrote:
> I heartily recommend using "coroutine", the proper denomination for the
> concept, which was introduced more than 40 years ago :)

Which concept, coroutines or threads?  They're not synonymous.

-g

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

David Given
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gregg Reynolds wrote:
> On 8/10/07, Ana Lucia Moura <[hidden email]> wrote:
>> I heartily recommend using "coroutine", the proper denomination for the
>> concept, which was introduced more than 40 years ago :)
> 
> Which concept, coroutines or threads?  They're not synonymous.

Just out of interest, under what situations would one use a Lua thread (usual
terminology from the manual), *without* having it also be a coroutine?

- --
âââ ïïïïïïïïïïïïïï âââ http://www.cowlark.com âââââââââââââââââââ
â
â "There does not now, nor will there ever, exist a programming language in
â which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGvLmSf9E0noFvlzgRAhZkAJ96NViZ7tYy1atZwA78qCxvdkSVfgCeJ8ud
+o5qUou3dYGfGBkGyX9pwRM=
=K165
-----END PGP SIGNATURE-----



Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Luiz Henrique de Figueiredo
> Just out of interest, under what situations would one use a Lua thread (usual
> terminology from the manual), *without* having it also be a coroutine?

http://www.lua.org/manual/5.1/manual.html#lua_xmove

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Gregg Reynolds-2
In reply to this post by David Given
On 8/10/07, David Given <[hidden email]> wrote:
>
> Just out of interest, under what situations would one use a Lua thread (usual
> terminology from the manual), *without* having it also be a coroutine?

I'm not sure; but see http://www.cs.princeton.edu/~diego/professional/luathread/

Which illustrates the confusion.  A "LuaThread" is not a "lua thread".
 Now suppose the developers decide in the future that it would be a
Good Thing to expose a true thread api as part of the language.
Doesn't seem likely, in light of
http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/hopl.pdf: "we did not (and
still do not) believe in the standard multithreading model, which is
preemptive concurrency with shared memory: we still think that no one
can write correct programs in a language where 'a=a+1' is
notdeterministic."  But who's counting?  "thread" is already taken; I
don't know if one could make it mean "kernel thread" without breakage.

In any case, the point is the current documentation tickles the
Principal of Least Surprise, by assigning a local meaning to a
well-known term.  Not that big of a deal, but we have evidence that
this is a source of confusion.

-g

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Ana Lucia Moura
In reply to this post by Gregg Reynolds-2


2007/8/10, Gregg Reynolds <[hidden email]>:
On 8/10/07, Ana Lucia Moura <[hidden email]> wrote:
> I heartily recommend using "coroutine", the proper denomination for the
> concept, which was introduced more than 40 years ago :)

Which concept, coroutines or threads?  They're not synonymous.

-g


Coroutines  (can be explained to the new generation as  "light  threads with no preemption" :)  )


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

David Given
In reply to this post by Gregg Reynolds-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gregg Reynolds wrote:
[...]
> "thread" is already taken; I
> don't know if one could make it mean "kernel thread" without breakage.

The embedded world tends to use 'task' to mean something in between a
traditional process and a traditional thread.

When writing another piece of software using coroutines (which eventually had
to be emulated using pthreads... sigh), I dubbed them 'threadlets'.

- --
âââ ïïïïïïïïïïïïïï âââ http://www.cowlark.com âââââââââââââââââââ
â
â "There does not now, nor will there ever, exist a programming language in
â which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGvMeXf9E0noFvlzgRAjXyAJ9fietxDOAtqShip3UDfEwCJgFCvgCeKlPl
cDVAJNhsk28w9Pz25EM6M0U=
=ElSF
-----END PGP SIGNATURE-----



Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Gregg Reynolds-2
> The embedded world tends to use 'task' to mean something in between a
> traditional process and a traditional thread.
>
> When writing another piece of software using coroutines (which eventually had
> to be emulated using pthreads... sigh), I dubbed them 'threadlets'.
>

Ah, the Joy of Lex.  Some other possibilities:  filament; rivulet,
rivage.  Rut?  It's always impressive to resort to Greek, Latin, or
some other archaic language.  The etymological dictionary says
"thread" comes from "thraedus", from base "thrae-", twist.  I rather
like "thraed" (pronounced "thrayd"?).  Like a thread, but not.  The
nice thing about it is that anybody who complains - as somebody surely
would - that it's mere 1337 play can be clobbered with a citation of
the etymology!

-g

Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

William Bubel
In reply to this post by John Klimek
The manual is quite clear on coroutines (section 2.11). coroutine.create produces an object of type "thread" that the coroutine functions operate on. The "coroutines" in this case would be the functions that make calls to coroutine.yield, although, type would return "function" for them.
Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Gregg Reynolds-2
> The manual is quite clear on coroutines (section 2.11). coroutine.create
> produces an object of type "thread" that the coroutine functions operate on.
> The "coroutines" in this case would be the functions that make calls to
> coroutine.yield, although, type would return "function" for them.

If it were clear, questions like "what's the difference between
threads and coroutines" would never arise.  The text may work for you,
but it's utterly misleading for me.  First, I can only know that Lua
uses an idiosyntractic meaning for "thread" if I happen to have read
and understood the other relevant sections of the manual.  Even worse,
the second sentence of 2.11 states:  "A coroutine in Lua represents an
independent thread of execution. "  Some may consider that clear, but
it's clear as mud to me, because "independent thread" strongly implies
to me that we're talking about multithreading (whether implemented in
kernel or user space).   (Even worse worse, look at the definition of
the type thread, which could not possibly be more obscure.)  In my
view Lua coroutines are by definition _not_ independent, since they
must cooperate.  It would be more accurate if not more clear to a)
remove all reference to threads, and b) call them distinct,
sequestered,  or the like, but not "independent".  Why not just use a
standard definition of coroutine?  E.g.  a coroutine is a subroutine
that supports suspend/resume operations and multiple entry points.  It
has nothing to do with threads, unless you're willing to say that
every routine (function) is a thread.  You could stipulate that every
jump represents a jump to a different "thread" but that seems kind of
pointless.

In any case, I'd say this horse is just about beaten to death.

Reply | Threaded
Open this post in threaded view
|

RE: What's the difference between a "coroutine" and a "thread" in Lua?

Jerome Vuarand-2
Gregg Reynolds wrote:
> If it were clear, questions like "what's the difference between
> threads and coroutines" would never arise.  The text may work for
> you, but it's utterly misleading for me.  First, I can only know that
> Lua uses an idiosyntractic meaning for "thread" if I happen to have
> read and understood the other relevant sections of the manual.  Even
> worse, the second sentence of 2.11 states:  "A coroutine in Lua
> represents an independent thread of execution. "  Some may consider
> that clear, but it's clear as mud to me, because "independent thread"
> strongly implies to me that we're talking about multithreading
> (whether implemented in         
> kernel or user space).   (Even worse worse, look at the definition of
> the type thread, which could not possibly be more obscure.)  In my
> view Lua coroutines are by definition _not_ independent, since they
> must cooperate.  It would be more accurate if not more clear to a)
> remove all reference to threads, and b) call them distinct,
> sequestered,  or the like, but not "independent".  Why not just use a
> standard definition of coroutine?  E.g.  a coroutine is a subroutine
> that supports suspend/resume operations and multiple entry points. 
> It has nothing to do with threads, unless you're willing to say that
> every routine (function) is a thread.  You could stipulate that every
> jump represents a jump to a different "thread" but that seems kind of
> pointless.          

I think you are confusing multithreading and preemptive multithreading.
Cooperative multithreading is a kind of multithreading. Any chunk of
code (function, subroutine, procedure, whatever...) that can be
interrupted, be it preemptively by the kernel or cooperatively by the
chunk itself, and later resumed is an independant thread (you doesn't
need anything but the thread itself to resume it).


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Adrien de Croy

I agree

in the context of OS threads, it refers to an independent call stack and context. Process memory is shared and accessible to all threads of a process. Stack variables however, belong to the thread whose stack they reside in.

re-entrable code vs yielding or being pre-empted are independent concepts to that of a thread.

Since a lua thread provices another call stack, and has its own context, I think it qualifies to be called a thread.

Jerome Vuarand wrote:
Gregg Reynolds wrote:
If it were clear, questions like "what's the difference between
threads and coroutines" would never arise.  The text may work for
you, but it's utterly misleading for me.  First, I can only know that
Lua uses an idiosyntractic meaning for "thread" if I happen to have
read and understood the other relevant sections of the manual.  Even
worse, the second sentence of 2.11 states:  "A coroutine in Lua
represents an independent thread of execution. "  Some may consider
that clear, but it's clear as mud to me, because "independent thread"
strongly implies to me that we're talking about multithreading
(whether implemented in kernel or user space). (Even worse worse, look at the definition of
the type thread, which could not possibly be more obscure.)  In my
view Lua coroutines are by definition _not_ independent, since they
must cooperate.  It would be more accurate if not more clear to a)
remove all reference to threads, and b) call them distinct,
sequestered,  or the like, but not "independent".  Why not just use a
standard definition of coroutine?  E.g.  a coroutine is a subroutine
that supports suspend/resume operations and multiple entry points. It has nothing to do with threads, unless you're willing to say that
every routine (function) is a thread.  You could stipulate that every
jump represents a jump to a different "thread" but that seems kind of
pointless.

I think you are confusing multithreading and preemptive multithreading.
Cooperative multithreading is a kind of multithreading. Any chunk of
code (function, subroutine, procedure, whatever...) that can be
interrupted, be it preemptively by the kernel or cooperatively by the
chunk itself, and later resumed is an independant thread (you doesn't
need anything but the thread itself to resume it).

--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

David Kastrup
Adrien de Croy <[hidden email]> writes:

> I agree
>
> in the context of OS threads, it refers to an independent call stack
> and context.

What do you mean with context?

> Process memory is shared and accessible to all threads of a process.
> Stack variables however, belong to the thread whose stack they
> reside in.
>
> re-entrable code vs yielding or being pre-empted are independent
> concepts to that of a thread.

Uh what?  A thread that is neither preempted nor yields is not a thread.

> Since a lua thread provices another call stack, and has its own
> context, I think it qualifies to be called a thread.

I don't see an "own context" in any sensible meaning of the word, and
of course coroutines all have their own stack.  That's the whole point
of them.  The difference between coroutines and threads is that
coroutines have a single flow of control.  That means that there is no
necessity for any locking in coroutines.  The sequence of operations
is uniquely determined by yielding.  That is no different to
subroutine calls, _except_ that yielding does not leave the call stack
of the current coroutine and reenters at the same place.

Essentially wrapping a function body into one endless loop with a
yield at the end is pretty much the same from a transfer of control
view than returning.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


Reply | Threaded
Open this post in threaded view
|

Re: What's the difference between a "coroutine" and a "thread" in Lua?

Adrien de Croy


David Kastrup wrote:
Adrien de Croy <[hidden email]> writes:

I agree

in the context of OS threads, it refers to an independent call stack
and context.

What do you mean with context?


in an OS thread, the context includes things like CPU registers etc.

Process memory is shared and accessible to all threads of a process.
Stack variables however, belong to the thread whose stack they
reside in.

re-entrable code vs yielding or being pre-empted are independent
concepts to that of a thread.

Uh what?  A thread that is neither preempted nor yields is not a thread.
a thread is a simple object. A scheduler pre-empts threads, the thread does not pre-empt itself.

So the concept of pre-empting or yielding are indeed independent concepts to the concept of a "thread". Related / linked maybe but they are their own concepts.

where did I say that a thread that is neither pre-empted NOR yields is not a thread? those are your words not mine. My words were "independent concepts". That does not imply mutual exclusivity, merely independence. Things can be independent without having to be mutually exclusive.
Since a lua thread provices another call stack, and has its own
context, I think it qualifies to be called a thread.

I don't see an "own context" in any sensible meaning of the word, and
of course coroutines all have their own stack.  That's the whole point
of them.  The difference between coroutines and threads is that
coroutines have a single flow of control.  That means that there is no
necessity for any locking in coroutines.  The sequence of operations
is uniquely determined by yielding.  That is no different to
subroutine calls, _except_ that yielding does not leave the call stack
of the current coroutine and reenters at the same place.

Essentially wrapping a function body into one endless loop with a
yield at the end is pretty much the same from a transfer of control
view than returning.


Actually the issue I was attempting to address was the question raised about whether it was correct to call a lua thread a thread at all. I believe it has the requirements to be validly called a thread.

--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com


123