4
votes

The Boost.Pool documentation says that (emphasis mine):

The Boost Pool library is a header-only library. That means there is no .lib, .dll, or .so to build; just add the Boost directory to your compiler's include file path, and you should be good to go!

But when I try to compile code like this in VS2010 SP1:

#include <string>
#include <vector>

#include <boost\pool\pool_alloc.hpp>

int main()
{
    typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, 
        boost::pool_allocator<wchar_t>> PoolString;

    std::vector<PoolString> vec;
    for (int i = 0; i < 100; i++)
    {
        PoolString s(L"Some test string. ABCDEF.");
        vec.push_back(s);
    }

    // Release pool memory
    boost::singleton_pool<boost::pool_allocator_tag, sizeof(wchar_t)>::release_memory();

    return 0;
}

I got a linker error:

error LNK1104: cannot open file 'libboost_thread-vc100-mt-gd-1_49.lib'

Is Boost.Pool documentation incorrect? What am I missing here? How can I use Boost.Pool?

3
The boost::singleton_pool is using a mutex type, which is located in the boost::thread library. Therefore you need to link libboost_thread. - nabulke
if you look in boost\pool\detail\mutex.hpp it appears that pool lib defines its own mutex and does not refer the boost::thread lib - Marius
@MariusBucur: I'll check again, to verify my answer. - nabulke
@nabulke Indeed boost::pool 1.49 uses the boost::thread, I was looking at version 1.47 which defines its own mutex (boost.org/doc/libs/1_47_0/boost/pool/detail/mutex.hpp). If linking with thread lib is not desired there is the option to use and older pool lib version. - Marius

3 Answers

2
votes

The boost::singleton_pool is using a default mutex implementation that is located in boost::thread, which is not header only.

See the singleton_pool header quoted below for information on how to remove the dependency:

Mutex This class is the type of mutex to use to protect simultaneous access to the underlying Pool. Can be any Boost.Thread Mutex type or boost::details::pool::null_mutex. It is exposed so that users may declare some singleton pools normally (i.e., with synchronization), but some singleton pools without synchronization (by specifying boost::details::pool::null_mutex) for efficiency reasons. The member typedef mutex exposes the value of this template parameter. The default for this parameter is boost::details::pool::default_mutex which is a synonym for either boost::details::pool::null_mutex (when threading support is turned off in the compiler (so BOOST_HAS_THREADS is not set), or threading support has ben explicitly disabled with BOOST_DISABLE_THREADS (Boost-wide disabling of threads) or BOOST_POOL_NO_MT (this library only)) or for boost::mutex (when threading support is enabled in the compiler).

0
votes

In boost/config/user.hpp:261 there is

//
// Turn threading support off if BOOST_DISABLE_THREADS is defined:
//
#if defined(BOOST_DISABLE_THREADS) && defined(BOOST_HAS_THREADS)
#  undef BOOST_HAS_THREADS
#endif

I think if you #define BOOST_DISABLE_THREADS before including it will solve it. After you will have to manage this option into your build system for your users maybe.

0
votes

From what I see the boost::pool dependency on thread lib was added in boost 1_48 (see 1.47 and 1.48

I think this is unfortunate, a better decision it would have been to use the header only lightweight mutex

You can change the pool\detail\mutex.hpp but probably it is simpler to link with the thread lib, especially if you are using other libs.