Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

What is the problem with writing singleton in multithreaded enviroment

Thread view: 
Ronen Yacov - 14 Jan 2007 08:54 GMT
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?
EJP - 14 Jan 2007 09:44 GMT
> When declaring a singleton, we write:
>
[quoted text clipped - 3 lines]
>     return instance;
> }

Not in Java we don't. Are you in the right newsgroup?
Arne Vajhøj - 14 Jan 2007 18:33 GMT
> When declaring a singleton, we write:
>
[quoted text clipped - 10 lines]
>
> 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/archive/2004/03/08/85901.aspx !

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

Arne
Tom Hawtin - 14 Jan 2007 19:41 GMT
> 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
Arne Vajhøj - 14 Jan 2007 19:59 GMT
>> 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
Tom Hawtin - 14 Jan 2007 20:10 GMT
>>> And double locking does not solve anything. Not in Java, not in C#
>>> and not in C++.
[quoted text clipped - 3 lines]
>
> It does not.

Does so.

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

Yes, correctly implemented it works correctly...

Tom Hawtin
Arne Vajhøj - 14 Jan 2007 20:14 GMT
>>>> And double locking does not solve anything. Not in Java, not in C#
>>>> and not in C++.
[quoted text clipped - 9 lines]
>
> Yes, correctly implemented it works correctly...

It is no longer the original double checked locking.

Arne
Lew - 14 Jan 2007 20:23 GMT
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...

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"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.