Boost logo

Boost :

Subject: Re: [boost] News about proposed Boost.Application library
From: Peter Dimov (lists_at_[hidden])
Date: 2014-07-29 06:14:43


Rob Stewart wrote:
> On July 29, 2014 3:39:16 AM EDT, "Klaim - Joël Lamotte"
> <mjklaim_at_[hidden]> wrote:
>
> >On 29 Jul 2014 02:33, "Rob Stewart" <robertstewart_at_[hidden]> wrote:
...
> >> It would be nice if get_symbol returned a smart pointer that contained
> >> a shared_ptr to the library. When all such references are released, the
> >> library can be unloaded.
> >
> >I disagree. In my experience it would be more useful if it contained a
> >weak_ptr to the library, with a centralised system keeping all loaded
> >libraries alive and allowing the used to unload the library explicitely.
...
> Please explain. Using a weak_ptr means each access must be tested, with
> synchronized access, to boot, versus assurance of validity with the
> shared_ptr.

It's possible to support both uses. If get_symbol is defined as

class library;

template<class F> shared_ptr<F> get_symbol( shared_ptr<library> pl )
{
    return shared_ptr<F>( pl, static_cast<F*>( ::dlsym( pl.get() ) ) );
}

it will return a shared_ptr that is an alias for the library. You could then
store the result in a weak_ptr, and it will expire when the library is
unloaded.

    shared_ptr<library> pl = load_library( "mylib.so" );
    weak_ptr<void()> pf = get_symbol( "myfunction" ); // void myfunction()

    pl.reset();
    assert( pf.expired() );


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