lua-users home
lua-l archive

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

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
unused threads. 

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
thread handle.

	InitSearchThread(LT, terms);
	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_pushstring(LT, "performSearch");
	lua_gettable(LT, LUA_GLOBALSINDEX); /* function to be called */
	lua_pushstring(LT, itsThread->mySearchTerms);
	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;
	return 0;

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
with it.

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