Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-07-26 07:51:20


Harling, Torsten wrote:

[...]

> Now try including the ace header first (only the order of
> the include files is changed):
> ------------------------------------------------------------
> #include <ace/OS.h>
> #include <boost/shared_ptr.hpp>
> #include <iostream>
>
> using namespace std;
>
> int main (int argc, char **argv)
> {
> cout << "sizeof (boost::detail::sp_counted_base) == "
> << sizeof (boost::detail::sp_counted_base) << endl;
> }
> ------------------------------------------------------------
>
> The output of the second program is:
> sizeof (boost::detail::sp_counted_base) == 36
>
> A great difference. The reason for this is, that the
> #include <ace/OS.h> somehow defines _REENTRANT, which
> triggers the boost library to include a mutex in
> sp_counted_base. I think, the order of the includes
> should not matter. Therefore, either '#include <ace/OS.h>'
> should not define _REENTRANT, or the boost headers
> should ignore it.
>
> I found out, that adding the -pthread option to the
> g++ command also avoids this bug.

If you compile

#include <boost/shared_ptr.hpp>
#include <iostream>

using namespace std;

int main (int argc, char **argv)
{
   cout << "sizeof (boost::detail::sp_counted_base) == "
       << sizeof (boost::detail::sp_counted_base) << endl;
}

(same program without the ACE include) you'll probably get the expected
outcome of 12 when -pthread isn't used and 36 when -pthread is used. This is
likely caused by g++ -pthread defining _REENTRANT, which is then used by
boost/config.hpp to enable threading support.

Since <boost/shared_ptr.hpp> doesn't know whether you're going to include an
ACE header afterwards, it has to decide whether to include a mutex based on
the present settings. If we make it ignore _REENTRANT, the above program
will revert to single-threaded mode even when -pthread is used, and this is
likely to break code.

The only practical solution seems to be to consistently use -pthread with
ACE, as I don't expect the ACE headers to stop defining _REENTRANT. They
probably have their own reasons for that and they don't need to support
single-threaded code.


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