This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: G++ and constructors
- From: John Love-Jensen <eljay at adobe dot com>
- To: Sean MacLennan <seanm at seanm dot ca>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Tue, 06 Mar 2007 06:44:03 -0600
- Subject: Re: G++ and constructors
Hi Sean,
> I don't need work-arounds, I just need to know if the code ever could be
> split.
Yes. The code can be split (assuming that "lock()" and "unlock()" are not
performing some thread "critical protection" locking for you).
Although you didn't ask for work-arounds, here's a work-around that works
for GCC, but may not work in C++ in general*.
GCC uses threading protection for static objects.
So this variant of your routine, in GCC, should be thread safe:
----------------------------------------------------------------------
myclass* myclass::init_instance() // private: static
{
return new myclass();
}
myclass* myclass::get_instance() // public: static
{
static myclass* instance = InitInstance();
return instance;
}
----------------------------------------------------------------------
HTH,
--Eljay
* since ISO 14882 C++ punted on the whole threading issue**.
** ... and on a standard C++ ABI, on modules, on... well, lots of stuff for
various reasons***.
*** some good reasons, some bad reasons -- depending on one's point of view.