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 &quot;lua-nakh.hxx&quot;<br>#include &l=
t;iostream&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;string.h&gt;<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 { &quot;SetordaneNakh&quot;, SetordaneNakh },<br>=C2=
=A0 =C2=A0 { &quot;NakheTaazeh&quot;, NakheTaazeh },<br>=C2=A0 =C2=A0 { &qu=
ot;BastaneNakh&quot;, 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&lt;&lt;=
&quot;L1&quot;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 matne_code =3D lua_tolstr=
ing(L, -1, nullptr);<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;L2&quot;&lt;&l=
t;std::endl;<br>=C2=A0 =C2=A0 lua_gettable(L, -1);<br>=C2=A0 =C2=A0 std::co=
ut&lt;&lt;&quot;L3&quot;&lt;&lt;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-&gt;L =3D luaL_newstate();<br>=C2=A0 =C2=A0 thr=
ead-&gt;thread_code =3D &amp;matne_code;<br><br>=C2=A0 =C2=A0 std::cout&lt;=
&lt;&quot;L4&quot;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 std::cout&lt;&lt;*thr=
ead-&gt;thread_code&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&qu=
ot;L5&quot;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 lua_pushlightuserdata(L, thr=
ead);<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;L6: &quot;&lt;&lt;(const void=
*)thread&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;L7&quot;=
&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 if(pthread_create(&am=
p;thread-&gt;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;&=
lt;&quot;Thread error&quot;&lt;&lt;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&lt;&lt;&quot;L=
8&quot;&lt;&lt;std::endl;<br><br>=C2=A0 =C2=A0 if(pthread_detach(thread-&gt=
;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;&lt;&quot;Thread error detach&quot;&lt;&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&lt;&lt;&quot;L9&quot;&lt;&lt;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-&gt;c_str(), matne_code-&gt;size(), &quot;th=
read1-L2&quot;) !=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&lt;&lt;&quot;loadbuffer() has errors. &quot;&lt;&lt;error&lt;&lt;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&lt;&lt;&quot;Post()&quot;&lt;&lt;std::endl;<br>*/<br><br>v=
oid* TheThread(void* arg) {<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;C1&quot=
;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 struct Thread* thread =3D (struct Thre=
ad*) arg;<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;C2: &quot;&lt;&lt;thread-=
&gt;thread_code-&gt;c_str()&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 //thread-&gt=
;L =3D luaL_newstate();<br>=C2=A0 =C2=A0 lua_State *L =3D thread-&gt;L;<br>=
=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;C3&quot;&lt;&lt;std::endl;<br>=C2=A0 =
=C2=A0 luaL_openlibs(L);<br>=C2=A0 =C2=A0 std::cout&lt;&lt;&quot;C4&quot;&l=
t;&lt;std::endl;<br>=C2=A0 =C2=A0 if(luaL_dostring(L, thread-&gt;thread_cod=
e-&gt;c_str()) !=3D 0)//LUA_OK)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout&lt=
;&lt;&quot;ERROR&quot;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 std::cout&lt;&lt;=
&quot;C5&quot;&lt;&lt;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&lt;&lt;=
&quot;ERROR pcall&quot;&lt;&lt;std::endl;<br>=C2=A0 =C2=A0 std::cout&lt;&lt=
;&quot;C6&quot;&lt;&lt;std::endl;<br><br>=C2=A0 =C2=A0 std::cout&lt;&lt;&qu=
ot;TheThread(): &quot;&lt;&lt;thread-&gt;thread_code-&gt;c_str()&lt;&lt;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, &quot;lua-nakh&quot;, 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&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/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--