Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading examples #69

Open
T4cC0re opened this issue Aug 28, 2019 · 5 comments
Open

Threading examples #69

T4cC0re opened this issue Aug 28, 2019 · 5 comments

Comments

@T4cC0re
Copy link

T4cC0re commented Aug 28, 2019

Hey there!

I would like to request some examples of a multi-threaded application. Specifically one, that does background tasks and then joins the threads later.

I tried to do this with both libNX threads and std::thread but I cannot get the threads to work reliably (i.e. not freeze the app at random times). I cannot easily post my code without massive modifications, so I would like to take a look at a reference implementation to see what I am doing wrong.

@fincs
Copy link
Contributor

fincs commented Aug 28, 2019

Standard threading APIs (i.e. pthreads, C threads, C++ threads) are all supported on the Switch (except for detach-type functionality which is not), so you can refer to any reputable programming resource that shows you how to use those. Everything should work fine; if there's something that doesn't then please let us see a test case in which it fails.

@T4cC0re
Copy link
Author

T4cC0re commented Aug 28, 2019

Okay, I'll try to port some code, that does not reliably work for me, so we can figure out why that is.

T4cC0re added a commit to T4cC0re/libnx-threads-freeze that referenced this issue Sep 2, 2019
@T4cC0re
Copy link
Author

T4cC0re commented Sep 2, 2019

I uploaded a repo with sample code and instructions here https://github.com/T4cC0re/libnx-threads-freeze

It would be great if you could take a look. Most of the time it just works, but sometimes it freezes after creating a thread. That might be a bug in libnx? I don't know. It also seems to be dependant on app start. Because a restart of the same code may either work or freeze.

@fincs
Copy link
Contributor

fincs commented Sep 2, 2019

    int i = 0;
    // Make the thread busy
    while (i < 10000) {
        i++;
    }
    return i * threadNr;

This will do nothing, as the compiler will optimize it out since the loop does nothing (it will turn it into just return 10000 * threadNr). Use volatile int to prevent this. Plus, this number of iterations is too low.

@T4cC0re
Copy link
Author

T4cC0re commented Sep 4, 2019

Hey @fincs Thanks for taking a look. I just pushed an update, that makes reproduction a bit easier. I also added a linux target to sanity check my code. And the weird thing is, that on Linux it works flawless. So there has to be something funky in HorizonOS, libnx or the switch hardware...

So I am guessing this is not the right place and this should rather be a libnx issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants