# Moving multithreading implementation within defold

**URL:** <https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094>\
**Category:** Questions\
**Created:** [February 26, 2024, 11:34am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094 "2024-02-26T11:34:52Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![CrazyAmphibian](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/crazyamphibian/32/26089_2.png) [@CrazyAmphibian](https://forum.defold.com/u/CrazyAmphibian)\
**Post date:** [March 2, 2024, 5:50am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/21 "2024-03-02T05:50:46Z")

</div>

for some reason, the string which contains the data to pass to the thread is corrupted sometime after the thread is created, and it is being executed.  
any leads?

it strikes me as odd since the function (which is stored in the same format) seems to be passed perfectly fine.

---

<div class="post-metadata">

**Author:** ![Chung\_Xa](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/chung_xa/32/34470_2.png) [@Chung\_Xa](https://forum.defold.com/u/Chung_Xa)\
**Post date:** [March 2, 2024, 6:27am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/22 "2024-03-02T06:27:30Z")

</div>

I experienced this, the string you get via `luaL_checkstring` is not safe, you better copy it using function such as `strncpy`

---

<div class="post-metadata">

**Author:** ![CrazyAmphibian](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/crazyamphibian/32/26089_2.png) [@CrazyAmphibian](https://forum.defold.com/u/CrazyAmphibian)\
**Post date:** [March 2, 2024, 10:47am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/23 "2024-03-02T10:47:28Z")

</div>

i don’t know if that’s exactly the case here, because that happens when i take numbers, too.  
and the function that converts the top-stack element to the data which is sent to a thread returns a `std::vector<char>`, which is then converted to `char* param` and `int paramlen`, stored in the struct which houses the other data for the thread. these varibles are correct until the thread is ran.

---

<div class="post-metadata">

**Author:** ![Chung\_Xa](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/chung_xa/32/34470_2.png) [@Chung\_Xa](https://forum.defold.com/u/Chung_Xa)\
**Post date:** [March 2, 2024, 11:04am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/24 "2024-03-02T11:04:25Z")

</div>

Yes, when the thread ran, the value passed to it via lua state will be unsafe to take after a while, esspecially char\*. It seems the buffer for it will be released and used for others

---

<div class="post-metadata">

**Author:** ![CrazyAmphibian](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/crazyamphibian/32/26089_2.png) [@CrazyAmphibian](https://forum.defold.com/u/CrazyAmphibian)\
**Post date:** [March 2, 2024, 11:18am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/25 "2024-03-02T11:18:16Z")

</div>

i’m afraid i don’t understand.  
i was under the impression that pushing variables to Lua from C didn’t change them in any way  
is this not true?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 2, 2024, 11:28am UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/26 "2024-03-02T11:28:27Z")

</div>

What he means is that you mustn’t mix the scopes of the Lua contexts.  
E.g. you mustn’t use something from a Lua context on the main thread, in another thread.  
For instance, if you get a `const char*` from Lua on the main thread, then that pointer is _not_ valid on another thread. Even if you don’t crash, it’s just luck.

So, that’s why we’re talking about “serializing” data or “copying” the data in a thread safe manner, so that the threads can communicate.

---

<div class="post-metadata">

**Author:** ![CrazyAmphibian](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/crazyamphibian/32/26089_2.png) [@CrazyAmphibian](https://forum.defold.com/u/CrazyAmphibian)\
**Post date:** [March 2, 2024, 9:07pm UTC](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094/27 "2024-03-02T21:07:46Z")

</div>

seems i’ve hit a dead end, then - i don’t know much about pointers or how memory works to make it functional.  
anyways here’s what code i had in case anyone else wants to look at it and/or try to do it (no, my code isn’t that good.).  
[multitedding.cpp](https://forum.defold.com/uploads/short-url/maig4ltK2E0CzevbS8OllhJXHM0.cpp) (8.6 KB)

[Previous page](https://forum.defold.com/t/moving-multithreading-implementation-within-defold/76094.md?page=1)
