QThread with control flow
-
@DungeonLords hi,
You don't and in fact, you don't need that goto statement. Just break the loop.
As for your sleep function, make it return a Boolean, and use it to do an early return and thus an early break..
-
@DungeonLords
No, there is not. You certainly cannot do any kind ofgoto
in C++ which is not local (i.e. label inside same function). This is not BASIC!For those functions which genuinely could/need to return an "error" result by all means make them
bool
. But applying that principle to every single level of function you call is not a good idea, makes code ugly.You can alternatively set a "global" (in some sense) flag and test from time to time, exiting functions if necessary. Like your
if(QThread::currentThread()->isInterruptionRequested())
does.Another possibility is to
emit
some signal at the bottom level if you have an event loop running somewhere. For example, a Qt UI program does not just stop dead and exit when user presses Close button, rather a signal is emitted, top-level event loop picks it up, cleans up and exits nicely.But none of these actually "unwinds the stack", as it is called for exiting from deep nesting. You still have to write code to test occasionally and react. The only way to do that is to use C++ *exceptions", i.e.
try ... catch
at top level andthrow()
at bottom level. But beware. This gets nasty with required C++ destruction of objects as their scope is terminated, can leave stuff in a "messy" state. Qt itself chooses not to use C++ exceptions, and I don't know how well it interacts if you put your own in. This answers your requirement but is probably not recommended.The real question/answer is: why do you suddenly want to exit/terminate a thread when you are down inside 10 levels of calls? You should look at your architecture/algorithm and avoid such necessity.
-
@JonB said in QThread with control flow:
The only way to do that is to use C++ *exceptions", i.e.
try ... catch
at top level andthrow()
at bottom level. But beware. This gets nasty with required C++ destruction of objects as their scope is terminated, can leave stuff in a "messy" state.I was waiting for someone else to introduce this unpleasantness. Thanks! (with a wink and a grin)
-
@jeremy_k
Well, it's the only way to truly do "non-local gotos with stack unwind" in C++ (per what the OP actually asks about)! And of course it's more common in languages other than C++. My recollection ofsetjmp
/longjmp()
is that they work even less well in C++, I think for example that MSVC has a compilation flag (/EH...
) you must pass if you want to use them or for other kinds of exceptions generally. And even if they do proper cleanup from C++ point of view, it would not surprise me if they could nonetheless leave your code in a bad state depending on just how the code handles objects, pointers etc. -
@JonB said in QThread with control flow:
@jeremy_k
Well, it's the only way to truly do "non-local gotos with stack unwind" in C++ (per what the OP actually asks about)! And of course it's more common in languages other than C++. My recollection ofsetjmp
/longjmp()
is that they work even less well in C++, I think for example that MSVC has a compilation flag (/EH...
) you must pass if you want to use them or for other kinds of exceptions generally. And even if they do proper cleanup from C++ point of view, it would not surprise me if they could nonetheless leave your code in a bad state depending on just how the code handles objects, pointers etc.Those links are documenting the standardized C++ variants, in the std namespace.
"On top of C longjmp, C++ std::longjmp has more restricted behavior."Yes, they are error prone. Exceptions are too. Locally scoped objects are destructed. Objects allocated with operator new and referenced by a normal pointer are not. Objects referenced via a unique_ptr are, unless the exception occurs between construction and assignment. Enter std::make_unique...
-
@jeremy_k
I think we are both happy to recommend OP not to introduce "error jumping". Nonetheless, I merely answered as to what would be required in C++. And if it did allow non-localgoto
s it would have the same "stack & object unwind" problems, and btw see that effect onswitch
/case
statements, which effectively dogoto
s and hence compiler places restrictions on what you can/cannot do about scope. "Don't shoot me I am only the messenger" ;-) -
@JonB said in QThread with control flow:
@jeremy_k
I think we are both happy to recommend OP not to introduce "error jumping".Unless I have a non-local goto out of the airplane, I'll agree.
Nonetheless, I merely answered as to what would be required in C++. And if it did allow non-local
goto
s it would have the same "stack & object unwind" problems, and btw see that effect onswitch
/case
statements,Yep. Just another syntax option while measuring out rope.
which effectively do
goto
s and hence compiler places restrictions on what you can/cannot do about scope.You're in danger of drifting into a Duff's device discussion.
"Don't shoot me I am only the messenger" ;-)
It may be too late for all of us!
-
@DungeonLords said in QThread with control flow:
@SGaist in my code I have function1() which call f2(), ... f12345(). And every function will return flag? Is there more beautiful way?
What exactly is your application doing ?
You seem to have quite a lot of chained functions calls which may be a sign of a too complex architecture especially if you do things in a thread that you want to break early from.
Knowing what you do will allow us to better help you. -
That library is a tool to implement something. What is that something ?
As stated earlier, if we know what you want to achieve, it will be easier to help you either integrate that library in your application or maybe use a different implementation.