c++ - Informative exit on stack overflow in worker thread -


I am writing a C ++ program that is running a bunch of worker thread on recursive data, such as' The default stack space has increased, it is possible that a thread can affect a stack overflow.

Ideally, the stack will need to be dynamically expanded, but if this is not possible, then it is acceptable that the program fails, and after re-compiling the user with a larger stack size To try again.

The problem with the default behavior of the crashing program with an error message is that the user has no way of knowing whether there was a problem or what to do about it; For all users, the program may try to divide by zero or a blank pointer can be removed; So if the program should crash, then I have to print this "stack overflow" first on stderr.

Obviously there will not be a solution in portable C ++, but I'm happy with a solution that works on Windows and the other that works on Linux.

By looking at how to exit the program with an informational error message on Windows, I am reading the document on curved and structured exception handling; One problem is that they look local to a thread, and can not write a thread to stderr; The best you get is the position of a race.

Is there a known way to deal with this?

OS (at least linux or unix flavor) allow you to catch stack mistakes.

Something like this:

  // Note: The printing call here is probably not a great idea, // It's well defined as we are in a signal handler What does that happen is the zero handler {FPrint F (stadder, "crash due to signal handler \ n"); Get out (42); }   

Then in the main or something like this ...

  struct sigaction sa = {handler, NULL, 0, 0, NULL}; Structure Signature Old; Signation (SIGTKFLT, SA, OLDSA);   

I will try to come up with a little more "complete" solution, with some experiments.

(I believe it's likely to change the stack, but I do not think you can continue to actually point at that point in a meaningful way, just by crashing you have more sensible ways !)

This works to work:

  #include & lt; Signal.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Iostream & gt; # Include & lt; Cstdlib & gt; Zero handler {write (2, "stack overflow \ n", 15); _exit (42); } Zero * two (zero * arg) {if (duh (arg)) {return zero (zero); } And {return duh (RGR); }} Zero * crash_wrapper (zero * arg) {static four stack [SIGSTKSZ]; Stack_t ss = {}; Ss.ss_sp = heap; Ss.ss_size = SIGSTKSZ; Sigaltstack (and SS, 0); Straight Signature Sa = {}; Sa.sa_handler = Handler; Sa.sa_flags = SA_ONSTACK, sigfilset (& sa.sa_mask); Signigation (SIGECGV, and SA, 0); Return duh (RGR); } Int main () {pthread_t t; Int position = pthread_create (& amp; t, 0, Crash_weeper, 0); (;;) {std :: cout & lt; & Lt; "Still going ..." & lt; & Lt; Std :: endl; Sleep (1); }}   

I am not completely happy with the writing inside the handler, but in all the other ways that I tried it did not seem to work either ...: (

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -