[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: Preemptive Threads, CloseThread (or the lack thereof)
- From: virgil@... (Virgil Smith)
- Date: Thu, 28 Aug 2003 10:41:34 -0500
Probably the first thing to ask yourself is whether or not you need Lua
Threads at all.
Given that separate lua_State's can be manipulated entirely separately (the
Lua core is re-entrant, i.e. it does not use any globals or statics, but
ONLY manipulates the supplied lua_State), you may not need to use Lua's
Thus if the only thread interaction you need can easily be handled by C
functions you can simply register them into multiple lua_States and leave
threading protection / interaction isolated to those C functions.
[mailto:email@example.com]On Behalf Of Jim Bumgardner
Sent: Thursday, August 28, 2003 3:29 AM
Subject: Preemptive Threads, CloseThread (or the lack thereof)
First off, hello to everybody on the list. I'm new here.
You may enjoy the Lua-powered screensavers I'm working on,
which are available here:
I'm currently using Lua for these screensavers, as well as for
making an image-processing tool for manipulating PNGs, JPEGs,
and Quicktime movies.
* * *
I have a question relating to the lack of an explicit function
for deleting/closing Lua threads, as has been discussed here before. Others
have pointed out that the reference manual
mentions a lua_closethread() but that it does not actually exist.
The consensus I get from reading prior messages is that
lua_closethread() is not necessary because lua garbage collects
I am using Windows threads to do purely preemptive threads, and I
don't understand how Lua can detect that my thread lua_State is
unused, since I am using it from C. More details follow.
I am using Lua in a windows screensaver. The lua code sometimes
asks the screensaver to begin a windows thread, which in turn,
calls a lua function. (The thread is used to perform an Internet
I'm currently accomplishing this as follows:
1. Lua calls C Function "CRequestThread"
2. CRequestThread creates a lua thread using:
lua_State *LT = lua_newthread(L);
and attaches it to a structure to be associated with a windows
gScopeThread = new ScopeThread((ScopeThreadRoutine) WinThreadFunction, LT);
3. gScopeThread attachs the LT to itself and creates a windows thread:
this->myLT = LT
mThreadH = CreateThread(NULL, 0, (ScopeThreadRoutine) WinThreadFunction,
this, 0, &mThreadID);
4. When the windows thread is executing (inside WinThreadFunction) a lua
is called, using LT, which is attached to the gScopeThread.
ScopeThreadReturnType ScopeThreadSearch(ScopeThread *itsThread)
lua_State *LT = itsThread->myLT;
lua_gettable(LT, LUA_GLOBALSINDEX); /* function to be called */
int result = lua_pcall(LT, 1, 1, 0); // 1 argument, 1 results
When lua returns from the function call, my thread sets a
notification flag and exits.
gSearchFound = nbrResults;
gSearchState = SS_Complete;
This is the point at which Lua should gc my thread, but I don't understand
how it could know that I am done with it. How does
it know that I'm not going to call other lua functions?
There are no references to LT in any of my lua objects in the
parent lua_State at any time, it is only referred to from C code.
Is there something extra I need to do so Lua has the information
it needs? I'm worried about causing memory leaks if I make
too many search threads. Alternatively, I worry that Lua
will prematurely garbage collect my thead when I'm not done
I noticed in someone else's example they used functions lua_cobegin() and
lua_yield(). I'm never using these, since I'm
using a preemptive model. Is there a reason I should be using cobegin or
- Jim Bumgardner