This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]