Segmentation fault: Lua and pthread

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Segmentation fault: Lua and pthread

Marc Vollmer
Hi all!

I have a problem with lua and threads. The C program starts, but after a
few loops the program terminates with segmentation fault. It's same
problems with linux and windows (access violation).

Can someone tell me why an segmentation fault happened? Or better, what
am I doing wrong?

Below I have attached the programs.

Thx
Marc

------------------------------------------
-- C program
------------------------------------------
#include <stdio.h>
#include <pthread.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

lua_State *masterState;
lua_State *workerState;
pthread_t a_thread;
int stop=0;

void *thread_function(void *arg){
  while (stop==0) {
    luaL_loadfile(workerState,"print.lua");
    lua_pcall(workerState, 0, 0, 0);
  }
  pthread_exit(0);
}

int main() {
  long loop;
  masterState = luaL_newstate();
  luaL_openlibs(masterState);
  workerState = lua_newthread(masterState);
  pthread_create(&a_thread,NULL, thread_function, NULL);
  for (loop=0; loop<1000000; loop++) {
    printf("Mem: %d\n",lua_gc(masterState,LUA_GCCOUNTB,0)*1024);
    luaL_loadfile(masterState,"fib.lua");
    lua_pcall(masterState, 0, 0, 0);
  }
  stop=1;
  printf("finish");
  return 0;
}

------------------------------------------
-- fib.lua
------------------------------------------
function fastfib(n)
  fibs={[0]=0, 1, 1} -- global variable, outside the function
  for i=3,n do
    fibs[i]=fibs[i-1]+fibs[i-2]
  end
  return fibs[n]
end
a=fastfib(10)
print("fastfib: "..a)

------------------------------------------
-- print.lua
------------------------------------------
for i=1,5 do
  print("lua loop "..i)
end

Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

Peter Cawley
Lua threads and C threads are very different things. Lua threads are
for co-operative multi-threading independently of the OS. The closest
OS equivalent to a Lua thread is a fibre. A Lua global state and it's
associated Lua threads should only be used by one OS thread at any
moment in time. If a global state is used by more than one OS thread
simultaneously, then segmentation faults will (eventually) occur.

On Fri, Jun 19, 2009 at 7:44 PM, Marc
Vollmer<[hidden email]> wrote:

> Hi all!
>
> I have a problem with lua and threads. The C program starts, but after a
> few loops the program terminates with segmentation fault. It's same
> problems with linux and windows (access violation).
>
> Can someone tell me why an segmentation fault happened? Or better, what
> am I doing wrong?
>
> Below I have attached the programs.
>
> Thx
> Marc
>
> ------------------------------------------
> -- C program
> ------------------------------------------
> #include <stdio.h>
> #include <pthread.h>
> #include "lua.h"
> #include "lauxlib.h"
> #include "lualib.h"
>
> lua_State *masterState;
> lua_State *workerState;
> pthread_t a_thread;
> int stop=0;
>
> void *thread_function(void *arg){
>  while (stop==0) {
>    luaL_loadfile(workerState,"print.lua");
>    lua_pcall(workerState, 0, 0, 0);
>  }
>  pthread_exit(0);
> }
>
> int main() {
>  long loop;
>  masterState = luaL_newstate();
>  luaL_openlibs(masterState);
>  workerState = lua_newthread(masterState);
>  pthread_create(&a_thread,NULL, thread_function, NULL);
>  for (loop=0; loop<1000000; loop++) {
>    printf("Mem: %d\n",lua_gc(masterState,LUA_GCCOUNTB,0)*1024);
>    luaL_loadfile(masterState,"fib.lua");
>    lua_pcall(masterState, 0, 0, 0);
>  }
>  stop=1;
>  printf("finish");
>  return 0;
> }
>
> ------------------------------------------
> -- fib.lua
> ------------------------------------------
> function fastfib(n)
>  fibs={[0]=0, 1, 1} -- global variable, outside the function
>  for i=3,n do
>    fibs[i]=fibs[i-1]+fibs[i-2]
>  end
>  return fibs[n]
> end
> a=fastfib(10)
> print("fastfib: "..a)
>
> ------------------------------------------
> -- print.lua
> ------------------------------------------
> for i=1,5 do
>  print("lua loop "..i)
> end
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

hoelzro
In reply to this post by Marc Vollmer
Hello Marc,

Lua threads aren't related to OS threads at all - they're just
coroutines.  Please read the chapter on coroutines (chapter 9 in PIL
v1), and check out LuaLanes or one of the other fine threading
libraries for all your Lua multithreading needs.  In short, it's
segfaulting because Lua and OS threads don't mix =(.

-Rob Hoelz

On Fri, 19 Jun 2009 20:44:41 +0200
Marc Vollmer <[hidden email]> wrote:

> Hi all!
>
> I have a problem with lua and threads. The C program starts, but
> after a few loops the program terminates with segmentation fault.
> It's same problems with linux and windows (access violation).
>
> Can someone tell me why an segmentation fault happened? Or better,
> what am I doing wrong?
>
> Below I have attached the programs.
>
> Thx
> Marc
>
> ------------------------------------------
> -- C program
> ------------------------------------------
> #include <stdio.h>
> #include <pthread.h>
> #include "lua.h"
> #include "lauxlib.h"
> #include "lualib.h"
>
> lua_State *masterState;
> lua_State *workerState;
> pthread_t a_thread;
> int stop=0;
>
> void *thread_function(void *arg){
>   while (stop==0) {
>     luaL_loadfile(workerState,"print.lua");
>     lua_pcall(workerState, 0, 0, 0);
>   }
>   pthread_exit(0);
> }
>
> int main() {
>   long loop;
>   masterState = luaL_newstate();
>   luaL_openlibs(masterState);
>   workerState = lua_newthread(masterState);
>   pthread_create(&a_thread,NULL, thread_function, NULL);
>   for (loop=0; loop<1000000; loop++) {
>     printf("Mem: %d\n",lua_gc(masterState,LUA_GCCOUNTB,0)*1024);
>     luaL_loadfile(masterState,"fib.lua");
>     lua_pcall(masterState, 0, 0, 0);
>   }
>   stop=1;
>   printf("finish");
>   return 0;
> }
>
> ------------------------------------------
> -- fib.lua
> ------------------------------------------
> function fastfib(n)
>   fibs={[0]=0, 1, 1} -- global variable, outside the function
>   for i=3,n do
>     fibs[i]=fibs[i-1]+fibs[i-2]
>   end
>   return fibs[n]
> end
> a=fastfib(10)
> print("fastfib: "..a)
>
> ------------------------------------------
> -- print.lua
> ------------------------------------------
> for i=1,5 do
>   print("lua loop "..i)
> end
>


signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

Duboucher Thomas
In llimits.h, you have also an interesting piece of code

#ifndef lua_lock
#define lua_lock(L)     ((void) 0)
#define lua_unlock(L)   ((void) 0)
#endif

#ifndef luai_threadyield
#define luai_threadyield(L)     {lua_unlock(L); lua_lock(L);}
#endif

I'm not 100% sure, but I remember that it's a way to have a thread safe
Lua librairy while using different Lua threads into different C threads.
The only drawback is that you need to rebuild your own Lua librairy.
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

Javier Guerra Giraldez
Duboucher Thomas wrote:

> In llimits.h, you have also an interesting piece of code
>
> #ifndef lua_lock
> #define lua_lock(L)     ((void) 0)
> #define lua_unlock(L)   ((void) 0)
> #endif
>
> #ifndef luai_threadyield
> #define luai_threadyield(L)     {lua_unlock(L); lua_lock(L);}
> #endif
>
> I'm not 100% sure, but I remember that it's a way to have a thread safe
> Lua librairy while using different Lua threads into different C threads.
> The only drawback is that you need to rebuild your own Lua librairy.

right, that's what LuaThread does.  it redefines those macros to make sure no related threads are run at the same time.


--
Javier
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

Marc Vollmer
In reply to this post by hoelzro
Hello Rob,

ok, now I have understood that lua threads and OS threads are different
things. But how I can use the LuaLanes in my C program? I find no
example of the API...

Marc


Am Freitag, den 19.06.2009, 13:53 -0500 schrieb Rob Hoelz:

> Hello Marc,
>
> Lua threads aren't related to OS threads at all - they're just
> coroutines.  Please read the chapter on coroutines (chapter 9 in PIL
> v1), and check out LuaLanes or one of the other fine threading
> libraries for all your Lua multithreading needs.  In short, it's
> segfaulting because Lua and OS threads don't mix =(.
>
> -Rob Hoelz
>
> On Fri, 19 Jun 2009 20:44:41 +0200
> Marc Vollmer <[hidden email]> wrote:


Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault: Lua and pthread

Sam Roberts
On Sat, Jun 20, 2009 at 5:21 AM, Marc Vollmer <[hidden email]> wrote:
ok, now I have understood that lua threads and OS threads are different
things. But how I can use the LuaLanes in my C program? I find no
example of the API...

Btw, have you seen luathread?


Reply | Threaded
Open this post in threaded view
|

Re[2]: Segmentation fault: Lua and pthread

Bulat Ziganshin
Hello Sam,

Saturday, June 20, 2009, 11:36:11 PM, you wrote:

> http://www.tecgraf.puc-rio.br/~diego/professional/luathread/

its reference contains funny typo: "Creates *and* a new cond, a
condition variable"

unfortunately i gave up trying to find any author's emails - only
phone number in CV :)


--
Best regards,
 Bulat                            mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Segmentation fault: Lua and pthread

Sam Roberts

On Sat, Jun 20, 2009 at 1:12 PM, Bulat Ziganshin <[hidden email]> wrote:
Hello Sam,

Saturday, June 20, 2009, 11:36:11 PM, you wrote:

> http://www.tecgraf.puc-rio.br/~diego/professional/luathread/

its reference contains funny typo: "Creates *and* a new cond, a
condition variable"

unfortunately i gave up trying to find any author's emails - only
phone number in CV :)

Diego has posted to this mailing list and is subscribed, posts here would probably reach him, or search the archive.

Sam