Boost logo

Boost Users :

Subject: Re: [Boost-users] at_thread_exit() never getting called
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-11-09 18:50:33


cowwoc wrote:
> PS: Is there a more reliable mechanism than atexit() that is
> guaranteed to support more than 32 hooks?

You could wrap the whole thing in a class, and use a static variable of that
class so that its destructor will be called on exit. Something like (not
compiled, not tested):

class my_thread
{
private:

    boost::scoped_ptr<boost::thread> pt_;
    bool shutdown_request_;
    // more per-thread state, if needed

    void threadproc(); // as before

public:

    my_thread(): shutdown_request_( false ) {}

    void start()
    {
        pt_.reset( new boost::thread( &my_thread::threadproc, this ) );
    }

    ~my_thread()
    {
        if( pt_ )
        {
            shutdown_request_ = true;
            pt_->join();
        }
    }
};

// ...

static my_thread s_th;

int main()
{

    s_th.start();
    // ...

} // the destructor of s_th should be called here


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net