[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Preemptive Threads, CloseThread (or the lack thereof)
- From: "Jim Bumgardner" <jbum@...>
- Date: Thu, 28 Aug 2003 01:28:41 -0700
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 function
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 yield?
- Jim Bumgardner