Re: when multi threading,"luaL_dostring" crashes in Lua 5.4, but works fine in Lua 5.1
Ali Rahbar <[email protected]> Tue, 21 Jul 2026 21:47:09 +0330
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CAJtdu10D3hqqFh=Vuw2CJzRQE-K4sO6zZt5DNTQvC1+GV=u9Jw@mail.gmail.com> |
--000000000000b9cd9e0657230868 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable oh sorry. i mistakely sent the code. the code is wrong. On Tue, Jul 21, 2026 at 12:21=E2=80=AFAM Roberto Ierusalimschy < [email protected]> wrote: > Would you mind explaining what this code is meant to show? > > > > #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[] =3D { > > { "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 =3D lua_tolstring(L, -1, nullptr); > > std::cout<<"L2"<<std::endl; > > lua_gettable(L, -1); > > std::cout<<"L3"<<std::endl; > > > > Thread* thread =3D new struct Thread; > > memset(thread, 0, sizeof(Thread)); > > thread->L =3D luaL_newstate(); > > thread->thread_code =3D &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) !=3D= 0) > { > > delete thread; > > std::cout<<"Thread error"<<std::endl; > > return -1; > > } > > std::cout<<"L8"<<std::endl; > > > > if(pthread_detach(thread->thread) !=3D 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") !=3D 0) > > { > > auto error =3D 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 =3D (struct Thread*) arg; > > std::cout<<"C2: "<<thread->thread_code->c_str()<<std::endl; > > //thread->L =3D luaL_newstate(); > > lua_State *L =3D thread->L; > > std::cout<<"C3"<<std::endl; > > luaL_openlibs(L); > > std::cout<<"C4"<<std::endl; > > if(luaL_dostring(L, thread->thread_code->c_str()) !=3D 0)//LUA_OK) > > std::cout<<"ERROR"<<std::endl; > > std::cout<<"C5"<<std::endl; > > //if(lua_pcall(L, 0, LUA_MULTRET, 0) !=3D 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_grSLLjqDnR9Q86LQHG2= MnDhd5Ny5%3DgvxypSGA%40mail.gmail.com > . > > > -- Roberto > > -- > 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/20260720205110.GB51566%40arraial.= inf.puc-rio.br > . > --=20 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 e= mail to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAJtd= u10D3hqqFh%3DVuw2CJzRQE-K4sO6zZt5DNTQvC1%2BGV%3Du9Jw%40mail.gmail.com. --000000000000b9cd9e0657230868 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">oh sorry. i mistakely sent the code. the code is= wrong.</div></div><br><div class=3D"gmail_quote gmail_quote_container"><di= v dir=3D"ltr" class=3D"gmail_attr">On Tue, Jul 21, 2026 at 12:21=E2=80=AFAM= Roberto Ierusalimschy <<a href=3D"mailto:[email protected]">robert= [email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_quote" = style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pa= dding-left:1ex">Would you mind explaining what this code is meant to show?<= br> <br> <br> > #include "lua-nakh.hxx"<br> > #include <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 =C2=A0return 0;<br> > }<br> > <br> > int BastaneNakh(lua_State* L) {<br> >=C2=A0 =C2=A0 =C2=A0return 0;<br> > }<br> > <br> > static luaL_Reg const lua_nakh_library[] =3D {<br> >=C2=A0 =C2=A0 =C2=A0{ "SetordaneNakh", SetordaneNakh },<br> >=C2=A0 =C2=A0 =C2=A0{ "NakheTaazeh", NakheTaazeh },<br> >=C2=A0 =C2=A0 =C2=A0{ "BastaneNakh", BastaneNakh },<br> >=C2=A0 =C2=A0 =C2=A0{ nullptr, nullptr }<br> > };<br> > <br> > static std::string matne_code;<br> > <br> > /* code text, shared table */<br> > int NakheTaazeh(lua_State* L) {<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L1"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0matne_code =3D lua_tolstring(L, -1, nullptr);<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L2"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0lua_gettable(L, -1);<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L3"<<std::endl;<b= r> > <br> >=C2=A0 =C2=A0 =C2=A0Thread* thread =3D new struct Thread;<br> >=C2=A0 =C2=A0 =C2=A0memset(thread, 0, sizeof(Thread));<br> >=C2=A0 =C2=A0 =C2=A0thread->L =3D luaL_newstate();<br> >=C2=A0 =C2=A0 =C2=A0thread->thread_code =3D &matne_code;<br> > <br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L4"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0std::cout<<*thread->thread_code<<std= ::endl;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L5"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0lua_pushlightuserdata(L, thread);<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L6: "<<(const voi= d*)thread<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L7"<<std::endl;<b= r> > <br> >=C2=A0 =C2=A0 =C2=A0if(pthread_create(&thread->thread, nullptr, = TheThread, thread) !=3D 0) {<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0delete thread;<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout<<"Thread error&q= uot;<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return -1;<br> >=C2=A0 =C2=A0 =C2=A0}<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L8"<<std::endl;<b= r> > <br> >=C2=A0 =C2=A0 =C2=A0if(pthread_detach(thread->thread) !=3D 0) {<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0delete thread;<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout<<"Thread error d= etach"<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return -2;<br> >=C2=A0 =C2=A0 =C2=A0}<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"L9"<<std::endl;<b= r> > <br> >=C2=A0 =C2=A0 =C2=A0return 1;<br> > }<br> > <br> > /*<br> >=C2=A0 =C2=A0 =C2=A0if (luaL_loadbuffer(L2, (const char *) matne_code-&= gt;c_str(),<br> > matne_code->size(), "thread1-L2") !=3D 0)<br> >=C2=A0 =C2=A0 =C2=A0{<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto error =3D lua_tostring(L2, -1);<= br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout<<"loadbuffer() h= as errors. "<<error<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0}<br> >=C2=A0 =C2=A0 =C2=A0lua_pcall(L2, 0, 0, 0);<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"Post()"<<std::end= l;<br> > */<br> > <br> > void* TheThread(void* arg) {<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C1"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0struct Thread* thread =3D (struct Thread*) arg;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C2: "<<thread->= ;thread_code->c_str()<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0//thread->L =3D luaL_newstate();<br> >=C2=A0 =C2=A0 =C2=A0lua_State *L =3D thread->L;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C3"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0luaL_openlibs(L);<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C4"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0if(luaL_dostring(L, thread->thread_code->c_st= r()) !=3D 0)//LUA_OK)<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout<<"ERROR"<= ;<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C5"<<std::endl;<b= r> >=C2=A0 =C2=A0 =C2=A0//if(lua_pcall(L, 0, LUA_MULTRET, 0) !=3D LUA_OK)<b= r> >=C2=A0 =C2=A0 =C2=A0//=C2=A0 =C2=A0 std::cout<<"ERROR pcall&= quot;<<std::endl;<br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"C6"<<std::endl;<b= r> > <br> >=C2=A0 =C2=A0 =C2=A0std::cout<<"TheThread(): "<<t= hread->thread_code->c_str()<<std::endl;<br> > <br> >=C2=A0 =C2=A0 =C2=A0return nullptr;<br> > }<br> > <br> > int luaopen_nakh(lua_State* L) {<br> >=C2=A0 =C2=A0 =C2=A0//luaL_newlib(L, lua_nakh_library);<br> >=C2=A0 =C2=A0 =C2=A0luaL_register(L, "lua-nakh", lua_nakh_lib= rary);<br> >=C2=A0 =C2=A0 =C2=A0return 1;<br> > }<br> > <br> > -- <br> > You received this message because you are subscribed to the Google Gro= ups "lua-l" group.<br> > To unsubscribe from this group and stop receiving emails from it, send= an email to <a href=3D"mailto:lua-l%[email protected]" target= =3D"_blank">[email protected]</a>.<br> > To view this discussion visit <a href=3D"https://groups.google.com/d/m= sgid/lua-l/CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5%3DgvxypSGA%40mail.gma= il.com" rel=3D"noreferrer" target=3D"_blank">https://groups.google.com/d/ms= gid/lua-l/CAJtdu10Yi1Cv_grSLLjqDnR9Q86LQHG2MnDhd5Ny5%3DgvxypSGA%40mail.gmai= l.com</a>.<br> <br> <br> -- Roberto<br> <br> -- <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:lua-l%[email protected]" target=3D"_= blank">[email protected]</a>.<br> To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/= lua-l/20260720205110.GB51566%40arraial.inf.puc-rio.br" rel=3D"noreferrer" t= arget=3D"_blank">https://groups.google.com/d/msgid/lua-l/20260720205110.GB5= 1566%40arraial.inf.puc-rio.br</a>.<br> </blockquote></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/CAJtdu10D3hqqFh%3DVuw2CJzRQE-K4sO6zZt5DNTQvC1%2BGV%3Du9Jw%40mail.gmai= l.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/m= sgid/lua-l/CAJtdu10D3hqqFh%3DVuw2CJzRQE-K4sO6zZt5DNTQvC1%2BGV%3Du9Jw%40mail= .gmail.com</a>.<br /> --000000000000b9cd9e0657230868--