when multi threading,"luaL_dostring" crashes in Lua 5.4, but works fine in Lua 5.1
Ali Rahbar <[email protected]> Mon, 20 Jul 2026 22:54:21 +0330
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5=gvxypSGA@mail.gmail.com> |
--00000000000048922606570fdbd1
Content-Type: text/plain; charset="UTF-8"
#include "lua-nakh.hxx"
#include <iostream>
#include <unistd.h>
#include <string.h>
void* TheThread(void*);
int SetordaneNakh(lua_State* L) {
return 0;
}
int BastaneNakh(lua_State* L) {
return 0;
}
static luaL_Reg const lua_nakh_library[] = {
{ "SetordaneNakh", SetordaneNakh },
{ "NakheTaazeh", NakheTaazeh },
{ "BastaneNakh", BastaneNakh },
{ nullptr, nullptr }
};
static std::string matne_code;
/* code text, shared table */
int NakheTaazeh(lua_State* L) {
std::cout<<"L1"<<std::endl;
matne_code = lua_tolstring(L, -1, nullptr);
std::cout<<"L2"<<std::endl;
lua_gettable(L, -1);
std::cout<<"L3"<<std::endl;
Thread* thread = new struct Thread;
memset(thread, 0, sizeof(Thread));
thread->L = luaL_newstate();
thread->thread_code = &matne_code;
std::cout<<"L4"<<std::endl;
std::cout<<*thread->thread_code<<std::endl;
std::cout<<"L5"<<std::endl;
lua_pushlightuserdata(L, thread);
std::cout<<"L6: "<<(const void*)thread<<std::endl;
std::cout<<"L7"<<std::endl;
if(pthread_create(&thread->thread, nullptr, TheThread, thread) != 0) {
delete thread;
std::cout<<"Thread error"<<std::endl;
return -1;
}
std::cout<<"L8"<<std::endl;
if(pthread_detach(thread->thread) != 0) {
delete thread;
std::cout<<"Thread error detach"<<std::endl;
return -2;
}
std::cout<<"L9"<<std::endl;
return 1;
}
/*
if (luaL_loadbuffer(L2, (const char *) matne_code->c_str(),
matne_code->size(), "thread1-L2") != 0)
{
auto error = lua_tostring(L2, -1);
std::cout<<"loadbuffer() has errors. "<<error<<std::endl;
}
lua_pcall(L2, 0, 0, 0);
std::cout<<"Post()"<<std::endl;
*/
void* TheThread(void* arg) {
std::cout<<"C1"<<std::endl;
struct Thread* thread = (struct Thread*) arg;
std::cout<<"C2: "<<thread->thread_code->c_str()<<std::endl;
//thread->L = luaL_newstate();
lua_State *L = thread->L;
std::cout<<"C3"<<std::endl;
luaL_openlibs(L);
std::cout<<"C4"<<std::endl;
if(luaL_dostring(L, thread->thread_code->c_str()) != 0)//LUA_OK)
std::cout<<"ERROR"<<std::endl;
std::cout<<"C5"<<std::endl;
//if(lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK)
// std::cout<<"ERROR pcall"<<std::endl;
std::cout<<"C6"<<std::endl;
std::cout<<"TheThread(): "<<thread->thread_code->c_str()<<std::endl;
return nullptr;
}
int luaopen_nakh(lua_State* L) {
//luaL_newlib(L, lua_nakh_library);
luaL_register(L, "lua-nakh", lua_nakh_library);
return 1;
}
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5%3DgvxypSGA%40mail.gmail.com.
--00000000000048922606570fdbd1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:tahoma,s=
ans-serif;font-size:large">#include "lua-nakh.hxx"<br>#include &l=
t;iostream><br>#include <unistd.h><br>#include <string.h><br=
><br>void* TheThread(void*);<br><br>int SetordaneNakh(lua_State* L) {<br>=
=C2=A0 =C2=A0 return 0;<br>}<br><br>int BastaneNakh(lua_State* L) {<br>=C2=
=A0 =C2=A0 return 0;<br>}<br><br>static luaL_Reg const lua_nakh_library[] =
=3D {<br>=C2=A0 =C2=A0 { "SetordaneNakh", SetordaneNakh },<br>=C2=
=A0 =C2=A0 { "NakheTaazeh", NakheTaazeh },<br>=C2=A0 =C2=A0 { &qu=
ot;BastaneNakh", BastaneNakh },<br>=C2=A0 =C2=A0 { nullptr, nullptr }<=
br>};<br><br>static std::string matne_code;<br><br>/* code text, shared tab=
le */<br>int NakheTaazeh(lua_State* L) {<br>=C2=A0 =C2=A0 std::cout<<=
"L1"<<std::endl;<br>=C2=A0 =C2=A0 matne_code =3D lua_tolstr=
ing(L, -1, nullptr);<br>=C2=A0 =C2=A0 std::cout<<"L2"<&l=
t;std::endl;<br>=C2=A0 =C2=A0 lua_gettable(L, -1);<br>=C2=A0 =C2=A0 std::co=
ut<<"L3"<<std::endl;<br><br>=C2=A0 =C2=A0 Thread* thr=
ead =3D new struct Thread;<br>=C2=A0 =C2=A0 memset(thread, 0, sizeof(Thread=
));<br>=C2=A0 =C2=A0 thread->L =3D luaL_newstate();<br>=C2=A0 =C2=A0 thr=
ead->thread_code =3D &matne_code;<br><br>=C2=A0 =C2=A0 std::cout<=
<"L4"<<std::endl;<br>=C2=A0 =C2=A0 std::cout<<*thr=
ead->thread_code<<std::endl;<br>=C2=A0 =C2=A0 std::cout<<&qu=
ot;L5"<<std::endl;<br>=C2=A0 =C2=A0 lua_pushlightuserdata(L, thr=
ead);<br>=C2=A0 =C2=A0 std::cout<<"L6: "<<(const void=
*)thread<<std::endl;<br>=C2=A0 =C2=A0 std::cout<<"L7"=
<<std::endl;<br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 if(pthread_create(&am=
p;thread->thread, nullptr, TheThread, thread) !=3D 0) {<br>=C2=A0 =C2=A0=
=C2=A0 =C2=A0 delete thread;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout<&=
lt;"Thread error"<<std::endl;<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 return -1;<br>=C2=A0 =C2=A0 }<br>=C2=A0 =C2=A0 std::cout<<"L=
8"<<std::endl;<br><br>=C2=A0 =C2=A0 if(pthread_detach(thread->=
;thread) !=3D 0) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 delete thread;<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 std::cout<<"Thread error detach"<&l=
t;std::endl;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return -2;<br>=C2=A0 =C2=A0 }<b=
r>=C2=A0 =C2=A0 std::cout<<"L9"<<std::endl;<br><br>=
=C2=A0 =C2=A0 return 1;<br>}<br><br>/*<br>=C2=A0 =C2=A0 if (luaL_loadbuffer=
(L2, (const char *) matne_code->c_str(), matne_code->size(), "th=
read1-L2") !=3D 0)<br>=C2=A0 =C2=A0 {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 a=
uto error =3D lua_tostring(L2, -1);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cou=
t<<"loadbuffer() has errors. "<<error<<std::end=
l;<br>=C2=A0 =C2=A0 }<br>=C2=A0 =C2=A0 lua_pcall(L2, 0, 0, 0);<br>=C2=A0 =
=C2=A0 std::cout<<"Post()"<<std::endl;<br>*/<br><br>v=
oid* TheThread(void* arg) {<br>=C2=A0 =C2=A0 std::cout<<"C1"=
;<<std::endl;<br>=C2=A0 =C2=A0 struct Thread* thread =3D (struct Thre=
ad*) arg;<br>=C2=A0 =C2=A0 std::cout<<"C2: "<<thread-=
>thread_code->c_str()<<std::endl;<br>=C2=A0 =C2=A0 //thread->=
;L =3D luaL_newstate();<br>=C2=A0 =C2=A0 lua_State *L =3D thread->L;<br>=
=C2=A0 =C2=A0 std::cout<<"C3"<<std::endl;<br>=C2=A0 =
=C2=A0 luaL_openlibs(L);<br>=C2=A0 =C2=A0 std::cout<<"C4"&l=
t;<std::endl;<br>=C2=A0 =C2=A0 if(luaL_dostring(L, thread->thread_cod=
e->c_str()) !=3D 0)//LUA_OK)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout<=
;<"ERROR"<<std::endl;<br>=C2=A0 =C2=A0 std::cout<<=
"C5"<<std::endl;<br>=C2=A0 =C2=A0 //if(lua_pcall(L, 0, LUA_=
MULTRET, 0) !=3D LUA_OK)<br>=C2=A0 =C2=A0 // =C2=A0 =C2=A0std::cout<<=
"ERROR pcall"<<std::endl;<br>=C2=A0 =C2=A0 std::cout<<=
;"C6"<<std::endl;<br><br>=C2=A0 =C2=A0 std::cout<<&qu=
ot;TheThread(): "<<thread->thread_code->c_str()<<std=
::endl;<br><br>=C2=A0 =C2=A0 return nullptr;<br>}<br><br>int luaopen_nakh(l=
ua_State* L) {<br>=C2=A0 =C2=A0 //luaL_newlib(L, lua_nakh_library);<br>=C2=
=A0 =C2=A0 luaL_register(L, "lua-nakh", lua_nakh_library);<br>=C2=
=A0 =C2=A0 return 1;<br>}<br><br></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;lua-l" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">lua-l+unsubsc=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
lua-l/CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5%3DgvxypSGA%40mail.gmail.co=
m?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/msgid=
/lua-l/CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5%3DgvxypSGA%40mail.gmail.c=
om</a>.<br />
--00000000000048922606570fdbd1--