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 &lt;<a href=3D"mailto:[email protected]">robert=
[email protected]</a>&gt; 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>
&gt; #include &quot;lua-nakh.hxx&quot;<br>
&gt; #include &lt;iostream&gt;<br>
&gt; #include &lt;unistd.h&gt;<br>
&gt; #include &lt;string.h&gt;<br>
&gt; <br>
&gt; void* TheThread(void*);<br>
&gt; <br>
&gt; int SetordaneNakh(lua_State* L) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0return 0;<br>
&gt; }<br>
&gt; <br>
&gt; int BastaneNakh(lua_State* L) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0return 0;<br>
&gt; }<br>
&gt; <br>
&gt; static luaL_Reg const lua_nakh_library[] =3D {<br>
&gt;=C2=A0 =C2=A0 =C2=A0{ &quot;SetordaneNakh&quot;, SetordaneNakh },<br>
&gt;=C2=A0 =C2=A0 =C2=A0{ &quot;NakheTaazeh&quot;, NakheTaazeh },<br>
&gt;=C2=A0 =C2=A0 =C2=A0{ &quot;BastaneNakh&quot;, BastaneNakh },<br>
&gt;=C2=A0 =C2=A0 =C2=A0{ nullptr, nullptr }<br>
&gt; };<br>
&gt; <br>
&gt; static std::string matne_code;<br>
&gt; <br>
&gt; /* code text, shared table */<br>
&gt; int NakheTaazeh(lua_State* L) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L1&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0matne_code =3D lua_tolstring(L, -1, nullptr);<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L2&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0lua_gettable(L, -1);<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L3&quot;&lt;&lt;std::endl;<b=
r>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0Thread* thread =3D new struct Thread;<br>
&gt;=C2=A0 =C2=A0 =C2=A0memset(thread, 0, sizeof(Thread));<br>
&gt;=C2=A0 =C2=A0 =C2=A0thread-&gt;L =3D luaL_newstate();<br>
&gt;=C2=A0 =C2=A0 =C2=A0thread-&gt;thread_code =3D &amp;matne_code;<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L4&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;*thread-&gt;thread_code&lt;&lt;std=
::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L5&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0lua_pushlightuserdata(L, thread);<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L6: &quot;&lt;&lt;(const voi=
d*)thread&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L7&quot;&lt;&lt;std::endl;<b=
r>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0if(pthread_create(&amp;thread-&gt;thread, nullptr, =
TheThread, thread) !=3D 0) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0delete thread;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;Thread error&q=
uot;&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return -1;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L8&quot;&lt;&lt;std::endl;<b=
r>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0if(pthread_detach(thread-&gt;thread) !=3D 0) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0delete thread;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;Thread error d=
etach&quot;&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return -2;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;L9&quot;&lt;&lt;std::endl;<b=
r>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0return 1;<br>
&gt; }<br>
&gt; <br>
&gt; /*<br>
&gt;=C2=A0 =C2=A0 =C2=A0if (luaL_loadbuffer(L2, (const char *) matne_code-&=
gt;c_str(),<br>
&gt; matne_code-&gt;size(), &quot;thread1-L2&quot;) !=3D 0)<br>
&gt;=C2=A0 =C2=A0 =C2=A0{<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto error =3D lua_tostring(L2, -1);<=
br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;loadbuffer() h=
as errors. &quot;&lt;&lt;error&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0 =C2=A0lua_pcall(L2, 0, 0, 0);<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;Post()&quot;&lt;&lt;std::end=
l;<br>
&gt; */<br>
&gt; <br>
&gt; void* TheThread(void* arg) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C1&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0struct Thread* thread =3D (struct Thread*) arg;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C2: &quot;&lt;&lt;thread-&gt=
;thread_code-&gt;c_str()&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0//thread-&gt;L =3D luaL_newstate();<br>
&gt;=C2=A0 =C2=A0 =C2=A0lua_State *L =3D thread-&gt;L;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C3&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0luaL_openlibs(L);<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C4&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0if(luaL_dostring(L, thread-&gt;thread_code-&gt;c_st=
r()) !=3D 0)//LUA_OK)<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;ERROR&quot;&lt=
;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C5&quot;&lt;&lt;std::endl;<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0//if(lua_pcall(L, 0, LUA_MULTRET, 0) !=3D LUA_OK)<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0//=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;ERROR pcall&=
quot;&lt;&lt;std::endl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;C6&quot;&lt;&lt;std::endl;<b=
r>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0std::cout&lt;&lt;&quot;TheThread(): &quot;&lt;&lt;t=
hread-&gt;thread_code-&gt;c_str()&lt;&lt;std::endl;<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0return nullptr;<br>
&gt; }<br>
&gt; <br>
&gt; int luaopen_nakh(lua_State* L) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0//luaL_newlib(L, lua_nakh_library);<br>
&gt;=C2=A0 =C2=A0 =C2=A0luaL_register(L, &quot;lua-nakh&quot;, lua_nakh_lib=
rary);<br>
&gt;=C2=A0 =C2=A0 =C2=A0return 1;<br>
&gt; }<br>
&gt; <br>
&gt; -- <br>
&gt; You received this message because you are subscribed to the Google Gro=
ups &quot;lua-l&quot; group.<br>
&gt; 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>
&gt; 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&quot; 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&quot; 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--