Lua usage tips (2)

Source: Internet
Author: User

In Lua script calls, if we run into a bad script, for example:

While 1 do

Do

End

The main thread of our program will also be blocked. So how do we prevent such a problem? Here is a workaround.

First, in order not to block the main thread, we are going to open a thread and put the processing of the script in this new working thread. (To learn more about the differences and management of worker threads and interface threads, see my other article blog, "Windows threading--Interface threads and worker threads" in Windows programming).

General idea:

1, the thread to perform script parsing, the following is Startrun ()

2, export a judge whether to end the function, let the script each loop is called, determine whether the thread is finished, this function is Isthreadexit (), Return a string ("exit" means that the while loop is over, "notexit" means that the thread is not finished)

3. Call Stoprun () if the main thread needs to actively end threads

4, it is best to export a ysleep pause function, so that the while loop to do too fast, resulting in high CPU consumption. http://hovertree.com/

In this way, the Lua script becomes the following:

While 1 do

Exitthread=isthreadexit ();
Ysleep (100);

if ExitThread = = "Exit" Then
Break
End

End

VC code is as follows, where the output is a function, you can use the MessageBox instead:

Global variables

Flag thread whether to end
BOOL g_bexitdofile = FALSE;
Thread handle
HANDLE g_hdofile = NULL;

Functions that need to be exported

Let Lua determine if the loop can exit
static int isthreadexit (lua_state* L)
{
if (g_bexitdofile)
Lua_pushlstring (L, "Exit", 4);
Else
Lua_pushlstring (L, "Notexit", 7);

A return value
return 1;
}

Pause function

int Ysleep (lua_state* L)
{
int d = Lual_checkinteger (L, 1);
Sleep (d);

return 0;
}

Register the above functions

int Regfunc ()

{
Lua_pushcfunction (G_plua, isthreadexit);
Lua_setglobal (G_plua, "isthreadexit");

Lua_pushcfunction (G_plua, ysleep);
Lua_setglobal (G_plua, "ysleep");

return 0;

}

Thread functions

DWORD WINAPI dofilethread (LPVOID lpparam)
{
LPCTSTR strFilePath = (LPCTSTR) Lpparam;
Lual_dofile (G_plua, strFilePath);

Stackdump (G_plua);

return 0;
}

Thread Start

int Startrun (LPCTSTR strFilePath)
{

Register all functions that need to be exported
Regfunc ();

if (G_hdofile = = NULL)
{
Creating threads to execute LUA scripts
G_hdofile = CreateThread (null, 0, Dofilethread, (LPVOID) strFilePath, 0, NULL);
}
Else
{
Output ("Please call Stoprun () first");
}

return 0;
}

Stopping a thread

int Stoprun ()
{
If the thread is running
if (g_hdofile)
{
Signals to the end of the LUA cycle
G_bexitdofile = TRUE;
Waiting for thread to exit
DWORD Dwret = WaitForSingleObject (G_hdofile, INFINITE);

If you exit successfully
if (Dwret = = wait_object_0)
{
CloseHandle (G_hdofile);
G_hdofile = NULL;
G_bexitdofile = FALSE;

Output ("Dofile thread exited!");
}
Otherwise hard to kill the thread
Else
{
DWORD Dwexitcode;
GetExitCodeThread (G_hdofile, &dwexitcode);

TerminateThread (G_hdofile, Dwexitcode);

G_hdofile = NULL;
G_bexitdofile = FALSE;

Output ("Dofile thread was killed!");
}
}

return 0;
}

The above method uses the thread's own characteristics to solve the problem of blocking the script. You can also use the hooks mechanism of LUA itself to prevent the blocking of scripts, this method next time ...

Http://www.cnblogs.com/roucheng/p/suanfa4.html

Lua usage tips (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.