Boost logo

Boost :

From: Hillel Y. Sims (hsims_at_[hidden])
Date: 2002-09-30 11:25:44


"David Abrahams" <dave_at_[hidden]> wrote in message
news:082f01c2670e$be11b5f0$6501a8c0_at_boostconsulting.com...
> OK. Sounds like the sort of thing that could be implemented at the library
> level, if only everyone would cooperate and use the facilities. Maybe we
> need a Boost error-handling library. Sounds like with all these
> nasty-C++-implementation-choices and thread cancellation safety issues, we
> probably do.
>

I think this is a great idea. May I suggest some possible entrants:

- ScopeGuard

- CancelGuard - disable thread-cancellation in critical regions
(platform-specific implementation: no effect on non-pthread platforms)

- NoThrowGuard - similar protection as "throw()" spec, but can be applied at
any block scope without need for interface-polluting ES, also provides
cancellation protection via CancelGuard. (
http://groups.google.com/groups?&th=e8754b0adf00544 )

-----
class CancelGuard : boost::noncopyable
{
  public:
    // pthread impl:
    CancelGuard()
      { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &m_origState); }
    ~CancelGuard()
      { pthread_setcancelstate(m_origState, 0); }

  private:
    int m_origState;
};

class NoThrowGuard : boost::noncopyable
{
  public:
    NoThrowGuard() : m_wasHandling(std::uncaught_exception())
      {}
    ~NoThrowGuard()
      {
         if (std::uncaught_exception() && !m_wasHandling)
            std::unexpected(); // (or maybe terminate()?)
      }

  private:
    CancelGuard m_cg;
    const bool m_wasHandling;
};

(I actually usually use CANCEL_GUARD and NOTHROW_REGION macros for
shortcuts, since they don't really need to be named objects.)

-----

thanks,
hys

--
Hillel Y. Sims
FactSet Research Systems
hsims AT factset.com

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