sneakyevil / IL2CPP_Resolver

A run-time API resolver for IL2CPP Unity.
https://sneakyevil.gitbook.io/il2cpp-resolver/
The Unlicense
362 stars 67 forks source link

Game deadlock on exit when using thread #35

Closed Shillersan closed 1 year ago

Shillersan commented 1 year ago
static void Update()
{
    while (true)
    {
        Sleep(1000);
       // do stuff
     }
}

I create a thread like this: IL2CPP::Thread::Create(&Update); then when I close the game, deadlock happens. I tried to using a variable instead of true and signalling the function to break on shutdown, didn't work.

extremeblackliu commented 1 year ago

creating own thread is not recommended and unstable, if you need a loop, please try use callback example in readme.md why you even need own thread? do you looking for a update function?

Shillersan commented 1 year ago

creating own thread is not recommended and unstable, if you need a loop, please try use callback example in readme.md why you even need own thread? do you looking for a update function?

I tried to use the callback but it was impacting the game's framerate significantly, and another reason is rather than every frame, I needed the method to execute once per second at most. The thread method was working fine at the first glance, but as you said it is kinda unstable and I couldn't think of a good way to shut it down gracefully before game exit.

extremeblackliu commented 1 year ago

~snip~

if you wanna execute your code in Update per second, you can literally go google for how does c# dev doing that. like you feel same experience as developing in unity, let me find you example with search in google with 'how to execute function per second in unity update': https://discussions.unity.com/t/how-to-call-a-function-every-second/169748 best way for you is using Time.deltaTime

Shillersan commented 1 year ago

~snip~

if you wanna execute your code in Update per second, you can literally go google for how does c# dev doing that. like you feel same experience as developing in unity, let me find you example with search in google with 'how to execute function per second in unity update': https://discussions.unity.com/t/how-to-call-a-function-every-second/169748 best way for you is using Time.deltaTime

Ok this makes sense, sorry for my ignorance but I wonder, is there a way to execute function without impacting the framerate? Like running async, but without going out of main thread.

Edit: After doing some research I believe there isn't, best way would be to optimize what I am doing and using just the Update callback.

extremeblackliu commented 1 year ago

i think you are looking for this: https://github.com/sneakyevil/IL2CPP_Resolver/issues/30