Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Robust development using 3rd party C libraries

Status
Not open for further replies.

alkuzo

Programmer
Feb 15, 2002
1
0
0
US
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 << &quot;FPE_handler /0\n&quot;;
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 << &quot;inside SIMPLECALL() before raise/0\n&quot; ;
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 << &quot;before SIMPLECALL\n&quot;;
SIMPLECALL();
cout << &quot;after SIMPLECALL\n&quot;;
checkError();
cout << &quot;after checkError();\n&quot;;
} catch(int i) {
cout << &quot;int caught inside main\n&quot;;
} catch (...) {
cout << &quot;the UNEXPECTED\n&quot;;
}
}
=======================================================
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



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top