C++ terminate called without an active exception
When a thread object goes out of scope and it is in joinable state, the program is terminated. The Standard Committee had two other options for the destructor of a joinable thread. It could quietly join -- but join might never return if the thread is stuck. Or it could detach the thread (a detached thread is not joinable). However, detached threads are very tricky, since they might survive till the end of the program and mess up the release of resources. So if you don't want to terminate your program, make sure you join (or detach) every thread.
std::thread - terminate called without an active exception, don't want to 'join' it
The trouble you are encountering is a result of the stopThread
going out of scope on the stack. The C++ standard has the following to say about this:
30.3.1.3 thread destructor [thread.thread.destr]
~thread();
If joinable() then terminate(), otherwise no effects. [ Note: Either
implicitly detaching or joining a joinable() thread in its destructor
could result in difficult to debug correctness (for detach) or
performance (for join) bugs encountered only when an exception is
raised. Thus the programmer must ensure that the destructor is never
executed while the thread is still joinable. — end note ]
What this means is that you should not let threads go out of scope without first calling either join()
or detach()
.
The way you describe it, you want the thread to go out of scope without joining so it will continue to run as your application runs. That requires a call to detach()
. From there, I can only offer a little wisdom...
That thread is now completely responsible for its own lifetime. If it doesn't return on its own, it will run forever (until the process terminates).
You are getting user input, presumably from something like
cin
orgetch()
. If these are accessed from multiple threads, you do not have much assurance that there are not race conditions in their library implementations. Tread lightly.
C++ Thread: terminate called without an active exception
Your code that creates the thread creates a stack variable that is immediately destroyed. You need to change this:
if(!isRepeatAllowed)
{
std::thread newThread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( &newThread);
}
to this:
if(!isRepeatAllowed)
{
std::thread* newThread = new std::thread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( newThread);
}
Then uncomment your delete line later on.
Terminate called without an active exception (throw in catch all expression)
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate()
may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...)
clause only catches C++ exceptions. In your particular case, terminate()
may be called within allocate_memory()
or clone()
directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
Related Topics
How to Deal with Global-Constructor Warning in Clang
Overriding Static Variables When Subclassing
Wrote to a File Using Std::Wofstream. the File Remained Empty
How to Extract the Mantissa of a Double
Why Does My Program Hang When Opening a Mkfifo-Ed Pipe
Passing Variable Number of Arguments with Different Type - C++
Sine Wave That Slowly Ramps Up Frequency from F1 to F2 for a Given Time
Cpu Dispatcher for Visual Studio for Avx and Sse
Somehow Register My Classes in a List
Arithmetic Right Shift Gives Bogus Result
Maximum Stack Size for C/C+ Program
The C 'Clock()' Function Just Returns a Zero
How to Enable Visual Styles Without a Manifest