Hi all,
could you please have a look at a problem I've come across.
I've developed a C++ module which makes an extensive use of DB2 CLI libraries.
My concern is:
By default, if the code inside the library raises a signal, like divide by 0, the whole program terminates.
Here is how I could catch signals and gracefully go on:
===========================================================================
#include
static int errorCode = 0;
void FPE_handler(int Ssignum)
{
cout << "FPE_handler /0\n";
errorCode++;
// throw (int)15;
//no way I could throw an exception from here. The specifications require plain C inside signal handlers.
// if I uncomment throw statement, it crashes.
}
void checkError()
{
if (errorCode > 0)
{
int ce = errorCode;
errorCode = 0;
throw ce;
}//if (errorCode > 0)
}
void SIMPLECALL()
{
//here is an imitation of a signal raised in a DB2
//CLI LIBRARY
cout << "inside SIMPLECALL() before raise/0\n" ;
raise(SIGINT);
}
int main(int argc, char *argv[])
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sa.sa_handler = FPE_handler;
sigaction(SIGINT, &sa, 0);
try
{
cout << "before SIMPLECALL\n";
SIMPLECALL();
cout << "after SIMPLECALL\n";
checkError();
cout << "after checkError();\n";
} catch(int i) {
cout << "int caught inside main\n";
} catch (...) {
cout << "the UNEXPECTED\n";
}
}
=======================================================
It works just fine in a single-threaded program.
After C function calls, I gotta check for errors anyway, so there isn't much overhead.
However, this won't work in multithreaded environment.
Could anybody please give me some advise how to catch C-type signals and raise C++ exceptions with thread safety?
I'll be most thanklful for any comments.
Alek
could you please have a look at a problem I've come across.
I've developed a C++ module which makes an extensive use of DB2 CLI libraries.
My concern is:
By default, if the code inside the library raises a signal, like divide by 0, the whole program terminates.
Here is how I could catch signals and gracefully go on:
===========================================================================
#include
static int errorCode = 0;
void FPE_handler(int Ssignum)
{
cout << "FPE_handler /0\n";
errorCode++;
// throw (int)15;
//no way I could throw an exception from here. The specifications require plain C inside signal handlers.
// if I uncomment throw statement, it crashes.
}
void checkError()
{
if (errorCode > 0)
{
int ce = errorCode;
errorCode = 0;
throw ce;
}//if (errorCode > 0)
}
void SIMPLECALL()
{
//here is an imitation of a signal raised in a DB2
//CLI LIBRARY
cout << "inside SIMPLECALL() before raise/0\n" ;
raise(SIGINT);
}
int main(int argc, char *argv[])
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sa.sa_handler = FPE_handler;
sigaction(SIGINT, &sa, 0);
try
{
cout << "before SIMPLECALL\n";
SIMPLECALL();
cout << "after SIMPLECALL\n";
checkError();
cout << "after checkError();\n";
} catch(int i) {
cout << "int caught inside main\n";
} catch (...) {
cout << "the UNEXPECTED\n";
}
}
=======================================================
It works just fine in a single-threaded program.
After C function calls, I gotta check for errors anyway, so there isn't much overhead.
However, this won't work in multithreaded environment.
Could anybody please give me some advise how to catch C-type signals and raise C++ exceptions with thread safety?
I'll be most thanklful for any comments.
Alek