Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change lua variable from C

Tags:

c

pthreads

lua

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.

like image 666
bislinux Avatar asked Jun 24 '15 12:06

bislinux


People also ask

Can you do ++ in Lua?

++ is not a C function, it is an operator. So Lua being able to use C functions is not applicable. Possible duplicate of stackoverflow.com/questions/7855525/….

Are Lua variables global?

In Lua, though we don't have variable data types, we have three types based on the scope of the variable. Global variables − All variables are considered global unless explicitly declared as a local.

What is Lua Userdata?

The lua_newuserdata function allocates a block of memory with the given size, pushes the corresponding userdatum on the stack, and returns the block address.


1 Answers

Interacting with a running lua state from a different thread is not necessarily safe so modifying the script's global variable may or may not be a useful idea depending on where you are planning to be making that change from the C side.

If you wanted to do this you would simply need to use the lua C api to set the global variable of the appropriate name in the appropriate lua state.

An alternate idea would be to create a should_exit global function which is called at the start or end of every loop and when it returns true causes the lua code to break or return. This function can then check anything it wants to on the C side in whatever thread-appropriate manner is desired.

like image 74
Etan Reisner Avatar answered Oct 07 '22 11:10

Etan Reisner