Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] thread specific variable instead of thread specific pointer?
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2009-07-13 08:53:02


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 12 July 2009, Frédéric Bron wrote:
> boost::thread_specific_ptr<std::ofstream> flogs ;
>
> // first solution using a function
> std::ostream &flog() { return *flogs.get() ; }
> // but I then have to add () after flog:
> flog() << "hello world!" ;
>
> // second solution using a macro
> #define flog flog()
> // but macros are not very recommended in C++.
>
> Any other idea that would allow me to write flog << "hello world!"
> like with std::cout?
>
> Note that I also tried to write a class derived from ostream with only
> one instance flog. This class had a special stream buffer using a
> thread specific pointer. However, the flog instance stores some
> informations like the precision of real numbers (flog <<
> setprecision(10) for example) and the output of one thread can then
> interfere with the output of other threads...

So put them all in the thread specific pointer. I don't see what's so
objectionable about flog() << "hello world!" though.

struct flogs_impl
{
        std::ofstream;
        unsigned precision;
        //...
};
class flogs
{
//...
        boost::thread_specific_ptr<flogs_impl> _impl;
};
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkpbLi4ACgkQ5vihyNWuA4W5LwCgwenyYt8MSADxvA4tVLNvsC4E
zFcAn2bjM0PW5NnewclxB5NbAXjTiDJa
=zGOO
-----END PGP SIGNATURE-----


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