I have a main program (in C) which needs to branch out into lua_thread(the main continues to run).This lua_thread calls a lua_script.lua. this lua_script contains a while loop. a lua variable controls this while loop.Currently this loop runs forever.
lua_script.lua
--this loop runs forever, as the exit value is not set yet
a=0
while(a<=0)
do
print("value of a:", a)
end
My goal is to change this lua variable(a) from main program such that it exits this infinite loop. Once this loop ends, it exits the thread and returns to the main program.
main.c
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
void *lua_thread()
{
int status, result;
double sum;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
status = luaL_loadfile(L, "lua_script.lua");
if (status)
{
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
result = lua_pcall(L, 0, 0, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_close(L);
return 0;
}
int main(void)
{
pthread_t p1;
pthread_create(&p1,NULL,lua_thread,NULL);
pthread_join(p1,NULL);
return 0;
}
If you run the above code
cc -o xcute main.c -I/usr/include/lua5.2 -llua -lm -ldl -pthread
it will go into an infinite loop. I want to somehow control the lua variable and change it to a=1,from the main program so that it comes out of the infinite loop. the reason for doing such a test is that it will make sure that before the main program exits, this thread exits first by controlling the lua variable. Please suggest how to change this lua variable so that it exits the while loop.