Construction of local static objects in a multithreaded environment 
Author Message
 Construction of local static objects in a multithreaded environment

Hi,

does anyone if (and how) the VC++ compiler protects the construction of
static objects declared at function level (see example below):

--------

int test()
{
    static MyClass the_class("Blaha");

    //
    // use the_class
    //
    int n = the_class.count();

    return n*2;

Quote:
}

--------

As I've understood it, a C++ compiler inserts code that checks if the_class
has been created, and if not, the object is created (i.e. the object is
created upon first usage).

But - now to my real question: Is this synchronized in some way when using
the multithreaded run-time? Otherwise the constructor of the object could be
called by several threads at the same time.

Anyone?

// Johan



Tue, 18 Feb 2003 15:54:05 GMT  
 Construction of local static objects in a multithreaded environment

Johan,

Quote:
> does anyone if (and how) the VC++ compiler protects the construction of
> static objects declared at function level (see example below):

> --------

> int test()
> {
>     static MyClass the_class("Blaha");

>     //
>     // use the_class
>     //
>     int n = the_class.count();

>     return n*2;
> }

> --------

> As I've understood it, a C++ compiler inserts code that checks if
the_class
> has been created, and if not, the object is created (i.e. the object is
> created upon first usage).

> But - now to my real question: Is this synchronized in some way when using
> the multithreaded run-time? Otherwise the constructor of the object could
be
> called by several threads at the same time.

Yep, that's a real {*filter*}, and can be a real pain to debug. So, to answer
your question, no, the compiler doesn't make any assumptions about the
environment, so it doesn't take threading into account.

That means you have to protect the object in some way, and that usually
means not making it a static variable. One way around it would be to use a
thread-safe implementation of the singleton pattern. There is a C/C++ Users
Journal on this very topic that was published 1 or 2 years ago.

--
Tomas Restrepo
http://www.*-*-*.com/



Tue, 18 Feb 2003 21:51:51 GMT  
 Construction of local static objects in a multithreaded environment
[inlined]


Quote:
> Johan,

> > does anyone if (and how) the VC++ compiler protects the construction of
> > static objects declared at function level (see example below):

[snip]

Quote:

> > But - now to my real question: Is this synchronized in some way when
using
> > the multithreaded run-time? Otherwise the constructor of the object
could
> be
> > called by several threads at the same time.

> Yep, that's a real {*filter*}, and can be a real pain to debug. So, to answer
> your question, no, the compiler doesn't make any assumptions about the
> environment, so it doesn't take threading into account.

> That means you have to protect the object in some way, and that usually
> means not making it a static variable. One way around it would be to use a
> thread-safe implementation of the singleton pattern. There is a C/C++
Users
> Journal on this very topic that was published 1 or 2 years ago.

I've considered this but this brings me to what I don't want to do: acquire
some kind of synchronization object each time I need a reference to the
object.

One way to avoid the problem is to explicitly call the function once, before
starting up any worker threads - but that doesn't really appeal to me.

I also got the stupid idea of using a CS stored within the object for
protection inside the constructor, but then - how can I be sure that I don't
initialize the CS member twice or more simultaneously, locking onto two
separate CS's. Well well - I guess I can't both have the cake and eat it :-(

Theres also another, unrelated (?) problem in this class. I constantly get
errors from the Win2k heap routines at program exit, when the static
object's destructor is called by the CRT. And I can tell you I've stepped
through this a zillion times without really finding the cause. It seems like
when the CRT's debugging heap management are writing the deadmansland fills,
the w2k heap functions doesn't like this - have you seen this before?

// Johan



Wed, 19 Feb 2003 03:44:23 GMT  
 Construction of local static objects in a multithreaded environment
Johan,

Quote:
> I've considered this but this brings me to what I don't want to do:
acquire
> some kind of synchronization object each time I need a reference to the
> object.

You don't need to pay that price if you use the double check pattern. In
other words, consider if your singleton Instance() creation function was
something like this:
T & T::Instance ( )
{
  if ( !m_instance )
  {
    m_lock.Acquire();
      if ( !m_instance )
        m_instance = new T();
    m_lock.Release();
  }
  return *m_instance;

Quote:
}

If you look carefully, you'll see you only need to acquire the lock if two
threads  simultanously step into the object's construction, which will only
happen once: when the singleton is really constructed. Once the object is
constructed, the only price you pay is a comparision.

Quote:
> Theres also another, unrelated (?) problem in this class. I constantly get
> errors from the Win2k heap routines at program exit, when the static
> object's destructor is called by the CRT. And I can tell you I've stepped
> through this a zillion times without really finding the cause. It seems
like
> when the CRT's debugging heap management are writing the deadmansland
fills,
> the w2k heap functions doesn't like this - have you seen this before?

I haven't seen that one.... maybe someone else can comment?

--
Tomas Restrepo [VC++ MVP]
http://www.mvps.org/windev



Wed, 19 Feb 2003 05:42:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How can I control static object construction order?

2. Call to local static object constructor not thread-safe

3. use local variables in a switch construction

4. Excessive page faults in multithreaded local server

5. Anyone worked in multithreaded environment with SQL Server?

6. Order of construction of static class members ?

7. Remote debugger failure in multithreaded environment VC toolkit 6.00

8. static construction clause

9. remote object Vs. local object

10. Object Construction

11. Construction order of members/global objects

 

 
Powered by phpBB® Forum Software