Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What is the problem with writing singleton in multithreaded enviroment

Reply
Thread Tools

What is the problem with writing singleton in multithreaded enviroment

 
 
Ronen Yacov
Guest
Posts: n/a
 
      01-14-2007
Hi everybody,

When declaring a singleton, we write:

static CMySingle::instance()
{
static CMySingle instance;
return instance;
}

Why should there be a problem in a multithreaded enviroment?
If one thread creates a static object the other threads will have the
same
instance (becuase its static).

And if there is a problem, how the docuble check locking solves it?

 
Reply With Quote
 
 
 
 
EJP
Guest
Posts: n/a
 
      01-14-2007
Ronen Yacov wrote:

> When declaring a singleton, we write:
>
> static CMySingle::instance()
> {
> static CMySingle instance;
> return instance;
> }


Not in Java we don't. Are you in the right newsgroup?
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      01-14-2007
Ronen Yacov wrote:
> When declaring a singleton, we write:
>
> static CMySingle::instance()
> {
> static CMySingle instance;
> return instance;
> }
>
> Why should there be a problem in a multithreaded enviroment?
> If one thread creates a static object the other threads will have the
> same
> instance (becuase its static).
>
> And if there is a problem, how the docuble check locking solves it?


That code is C++ not Java.

But anyway. The problem is that the C++ standard mandates that
the constructor can not be called before the first call to
the method. This makes it a non atomic operation.

For some details see
http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx !

And double locking does not solve anything. Not in Java, not in C#
and not in C++.

Arne
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      01-14-2007
Arne Vajh°j wrote:
>
> And double locking does not solve anything. Not in Java, not in C#
> and not in C++.


Double-checked locking? Works fine in Java (1.5+, and in practice 1.4+),
so long as you do it right.

Tom Hawtin
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      01-14-2007
Tom Hawtin wrote:
> Arne Vajh°j wrote:
>> And double locking does not solve anything. Not in Java, not in C#
>> and not in C++.

>
> Double-checked locking? Works fine in Java (1.5+, and in practice 1.4+),
> so long as you do it right.


It does not.

It only works in the modified form with the volatile keyword.

Arne
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      01-14-2007
Arne Vajh°j wrote:
> Tom Hawtin wrote:
>> Arne Vajh°j wrote:
>>> And double locking does not solve anything. Not in Java, not in C#
>>> and not in C++.

>>
>> Double-checked locking? Works fine in Java (1.5+, and in practice
>> 1.4+), so long as you do it right.

>
> It does not.


Does so.

> It only works in the modified form with the volatile keyword.


Yes, correctly implemented it works correctly...

Tom Hawtin
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      01-14-2007
Tom Hawtin wrote:
> Arne Vajh°j wrote:
>> Tom Hawtin wrote:
>>> Arne Vajh°j wrote:
>>>> And double locking does not solve anything. Not in Java, not in C#
>>>> and not in C++.
>>>
>>> Double-checked locking? Works fine in Java (1.5+, and in practice
>>> 1.4+), so long as you do it right.

>>
>> It does not.

>
> Does so.
>
>> It only works in the modified form with the volatile keyword.

>
> Yes, correctly implemented it works correctly...


It is no longer the original double checked locking.

Arne
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-14-2007
Arne Vajh°j wrote:
>> And double locking does not solve anything. Not in Java, not in C#
>> and not in C++.


Tom Hawtin wrote:
> Double-checked locking? Works fine in Java (1.5+, and in practice
> 1.4+), so long as you do it right.


>> It does not.


> Does so.


>> It only works in the modified form with the volatile keyword


> Yes, correctly implemented it works correctly...


Time out. You guys agree - double-checked locking works, if all restrictions
and limitations are observed. Not if used "by itself", which some might call
"incorrectly", but if used "modified, with volatile", which some might call
"correctly".

- Lew "the Peacemaker"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
postponed Initialization in multithreaded enviroment. George ASP .Net 9 10-27-2008 10:08 PM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
What is the problem with writing singleton in multithreaded enviroment Ronen Yacov C++ 3 01-15-2007 07:37 PM
Re: cgi "print statement" in multithreaded enviroment? vegetax Python 1 05-03-2005 02:35 PM
cgi "print statement" in multithreaded enviroment? vegetax Python 5 05-03-2005 02:31 AM



Advertisments