Boost logo

Boost :

From: Malcolm Noyes (boost_at_[hidden])
Date: 2004-06-10 23:56:14


During my 'research' into different ways to implement thread specfic
storage (tss) on Win32, I needed to implement custom cleanup handlers.
The thread_dev branch allows for a custom cleanup handler on the basis
of 'one cleanup handler per global object instance'. This is *not*
the same as 'one cleanup handler per tss object instance' since with
tss there is of course one object instance per thread. Consider:

void no_cleanup(int*)
{
    // do nothing
}

thread_specific_ptr p(no_cleanup);

int main(int, char*[])
{
    int some_number = 42;

    // leaving aside why someone might want to do this, it's
    // the mechanincs that we're interested in!

    p.reset(&some_number); // fine

    p.reset(new int(0)); // leak - 'p' has no cleanup!
    return 0;
}

The theoretical alternative would be to allow (enforce?) a different
cleanup handler per thread object, e.g.:

void no_cleanup(int*)
{
    // do nothing
}

void delete_cleanup(int* pd)
{
    delete pd;
}

thread_specific_ptr p;

int main(int, char*[])
{
    int some_number = 42;

    // this time call reset with a specfic handler for this thread
    //object instance

    p.reset(&some_number, no_cleanup); // fine

    p.reset(new int(0), delete_cleanup); // ok now, will call delete
    return 0;
}

My feeling is that boost::thread should only allow the first form and
document that the use above leads to unspecified behaviour (it may not
be a memory leak, we don't know what the cleanup handler may do). My
only reason for this however is that allowing the latter case is
probably not a frequent use and makes the implementation much
complicated. Does anyone think the latter is required ?

Malcolm
Malcolm Noyes


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