Boost logo

Boost :

Subject: [boost] structured exceptions for UNIXs -- the hard way
From: Peter Foelsche (foelsche_at_[hidden])
Date: 2011-09-20 17:04:02


On UNIXs one does not get around signals.
They are used for everything.
E.g. using memory mapped io is impossible without dealing with signals.
E.g. somebody may edit the file which is mapped into your process
thereby truncating it and invalidating some part of the memory.
Or one may be writing into a sparse file via memory mapped io
and the result of "out of disk space" is also a signal.

Then there are such sick solutions as setjmp()/longjmp().
Could they be packed into some class to enable some kind of structured exceptions on UNIXs?

#include <setjmp.h>
#include <vector>

static std::vector<jmp_buf> s_sStack;

static void signalHandler(void)
{ if (s_sStack.size())
        { jmp_buf s = s_sStack.back();
                s_sStack.pop_back();
                longjmp(s);
        }
}

struct CSetLongJmp
{ CSetLongJmp(void)
        { s_sStack.push_back(jmp_buf());
                if (setjmp(s_sStack.back()))
                          throw "something";
        }
        ~CSetLongJmp(void)
        { s_sStack.pop_back();
        }
};

This class would need to be part of anything which needs to be protected against signals:

std::string s;
CSetLongJmp s1;
useString(s);

Peter Foelsche


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk