-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
ofThread : short lived threads can cause uncaught exceptions #5262
Comments
can you check which kind of exception is throwing? a debugger should tell you that |
thread::detach failed: Invalid argument |
i tried checking if its joinable() b4 detaching first, but it happens too. |
yes it's better to check the exception since the thread could be joinable when checking but become not joinable after. can you send a PR? |
Fix for #5262 - Short lived threads can cause exceptions
fixed in #5263 |
I am going to reopen this; I finally found some time to really look into it and it still is an issue. When you launch several short-lived ofThreads too suddenly (i.e. spawn a few threads in a single frame), sometimes the thread gets in a weird state. I have an example demonstrating the issue here. I have seen the issue both on OSX and windows. If you run the code above, you will see printouts like this:
every few seconds. This is as far as I got with this, basically the thread object is done (and it executed fine) but the std::thread itself is in a weird state, so if you try to delete it it leads to crash. I think it is related to the way the ofThread is launched with :
interacting with the This is a pretty big stability problem for one of my addons; I worked around the problem by creating a simplified ofThread-like class that works 100% ( left it running for weeks ). You can see this alternate ofThread class here. |
if you change also opening a lot of threads in a short period of time doesn't sound like a good idea, since it's a really expensive operation, not saying we shouldn't fix this because of that, this is clearly a bug, but if you need to use several threads in an application you probably want to create a pool of threads and send tasks to then instead or simply use std::async which should use a pool of threads behind the scenes |
No, changing the example to Playing a bit more with the example, the cause seems to be the The original reason for this issue caused the app to crash bc The issue seems partly caused because detach() is called from within the thread itself; if I call detach right away from the main thread after creating it, the problem disappears that's the main difference in my alternative ofThread implementation here). I've been searching for docs on It seems everything is easier if you to know what you will do with the thread before you spawn it (and I mean "what will you do" in terms of cleanup strategy; will you join it or will you detach it?). We added My current solution only works for me because I know beforehand that I will not be joining the thread I'm spawning; thus I can afford to detach it safely from the main thread as soon as I spawn it. As it seems detaching from the main thread rises no issue, I wonder if an ofThread API change enforcing the user to spawn the thread explicitly stating a cleanup strategy would be to ask too much for the user - because doing so would solve the problem. I could see something along the lines of In the other case, if the user starts a thread telling ofThread that he/she plans to join the thread, we can issue detect a broken promise and issue warning if the threadedFunction() exits and the This may be a lot to ask for the users - but I believe this will also make the users more aware about the internals of threads and general app architecture, which I would argue is beneficial. I think adding an example demonstrating the two cases would also make things much more clear. We should probably leave the current API as it is with unchanged behaviour for backwards compatibility, and add two alternative spawn methods i.e. PD: Yes the overhead on this example is ludicrous but I merely built it to be able to reproduce the issue. I have a centralised queue that limits on how many requests (i.e. threads) can happen per frame, but because I also have other threads spawning across other areas of the app (and the modules aren't really aware of each other), the issue still arises from time to time when a few spawn too "close" to each other. |
I am getting sporadic crashes on very short lived threads; This code is enough to reproduce the issue; at about one crash per minute. I am seeing this happen on OS X 10.11.
It seems that every now and then the thread fails to detach() and throws an exception.
openFrameworks/libs/openFrameworks/utils/ofThread.cpp
Line 172 in 88545f1
Wrapping the call in a try/catch keeps the app running.
The text was updated successfully, but these errors were encountered: