HighTechTalks DotNet Forums  

Singleton and Local static object:

VC++.net microsoft.public.dotnet.languages.vc


Discuss Singleton and Local static object: in the VC++.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Alamelu
 
Posts: n/a

Default Singleton and Local static object: - 12-28-2008 , 02:57 AM






Is the below code thread safe, in multithreaded environments will a race
condition happen as we are using static

Singleton* Singleton::Instance ()
{
static Singleton s;
return &s;
}

Regards,
Alamelu

Reply With Quote
  #2  
Old   
Brian Muth
 
Posts: n/a

Default Re: Singleton and Local static object: - 12-29-2008 , 11:49 AM






That particular code segment is thread-safe. However, if you want to actually update the contents of the Singleton class, you need
to make sure you write that code in a thread-safe manner.

"Alamelu" <Alamelu (AT) discussions (DOT) microsoft.com> wrote

Quote:
Is the below code thread safe, in multithreaded environments will a race
condition happen as we are using static

Singleton* Singleton::Instance ()
{
static Singleton s;
return &s;
}

Regards,
Alamelu


Reply With Quote
  #3  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: Singleton and Local static object: - 12-30-2008 , 04:57 PM



C++ makes no guarantees in multithreaded environments. So the
initialization of a static object could easily have a race condition. C++0x
will address parallel execution for the first time.

"Brian Muth" <bmuth (AT) mvps (DOT) org> wrote

Quote:
That particular code segment is thread-safe. However, if you want to
actually update the contents of the Singleton class, you need to make sure
you write that code in a thread-safe manner.

"Alamelu" <Alamelu (AT) discussions (DOT) microsoft.com> wrote in message
news:731CBBAD-9656-4810-8137-E696514A7C4A (AT) microsoft (DOT) com...
Is the below code thread safe, in multithreaded environments will a race
condition happen as we are using static

Singleton* Singleton::Instance ()
{
static Singleton s;
return &s;
}

Regards,
Alamelu


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.