Re: G++ and constructors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux