Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
Change lua variable from C

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.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

Why to have this loop in Lua? You may loop in c-thread instead, lua_pcalling some entry-point function (e.g. onEvent()) on each iteration.

If loop has to be in Lua script, for example in case of setup-loop-cleanup scheme, you may run script in coroutine and use coroutine.yield() as loop condition. Thread should lua_resume() with true value or exit depending on your c-side condition. (Or resume with false if Lua-side cleanup after the loop is preferred.)

Anyway, Lua is not thread-safe and cannot be called simultaneously from more than one thread.

over 4 years ago · Santiago Trujillo Relatório

0

Oh, you went hard way in your answer (sure that was a great exercise though). Things could be much simpler:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static volatile int shouldRun = 1; // 1.

static int
fn_sleep(lua_State *L)
{
    lua_Integer n = luaL_checkinteger(L, 1);
    sleep(n < 0 ? 0 : n);
    return 0;
}

static int
fn_shouldRun(lua_State *L)
{
    lua_pushboolean(L, shouldRun);
    return 1;
}

static void *
thread_main(void *dummy)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    lua_register(L, "sleep", fn_sleep); // 2.
    lua_register(L, "shouldRun", fn_shouldRun);

    if (luaL_dofile(L, "script.lua")) {
        fprintf(stderr, "%s\n", lua_tostring(L, -1));
        lua_pop(L, 1);
    }

    lua_close(L); // 3.
    return 0;
}

int
main(int argc, char *argv[])
{
    pthread_t thread;
    pthread_create(&thread, NULL, thread_main, NULL);

    sleep(5);
    shouldRun = 0; // 1.

    pthread_join(thread, NULL);
    return 0;
}

script.lua:

print("Hi!")

while shouldRun() do
    print("Running!")
    sleep(1)
end

print("Bye!")

output:

Hi!
Running!
Running!
Running!
Running!
Running!
Bye!

Few things to note:

  1. Polled exit conditions usually do not need any protection. If shouldRun() was called simultaneously with variable update and missed it, then nothing to worry about – it will return false next time. Anyway, you can't even guess where the script is executing, just [re]set the value and wait for completion.
  2. For a pair of specific functions there is no need to write a require/preload-compatible module. Export them into global namespace instead.
  3. For graceful close on error do not just exit(). lua_close() will garbage-collect all userdata used by script, and their __gc metamethod may do something useful like flushing buffers to disk, etc. You lose all chances with hard exit.
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda