By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,978 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,978 IT Pros & Developers. It's quick & easy.

Atomic and singleton

P: n/a
Hello,

Is a singleton class the same as an atomic class? I know that a singleton class
can only be instantiated once, but does that concept apply to an atomic class?

Thank you.
Aug 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Freedom fighter wrote:
Hello,

Is a singleton class the same as an atomic class? I know that a singleton class
can only be instantiated once, but does that concept apply to an atomic class?
Atomic usually refers to multi-threaded applications. Atomic would mean
that any operation performed on the class would happen in such a way as
the only "visible" states were the state prior to an operation and the
state after the operation was complete. Intermediate states would not
be visible to any other threads.

A classic is an "atomic" int. e.g.

-------------------------------
int val = 0;

int f()
{
return ++ val; // not atomic ...
}
-------------------------------
atomic_int val = 0;

int f()
{
return ++ val; // is atomic
}
-------------------------------

In the first example, "++ val" needs to read, increment and write, not
to mention issues with cache ceherency. In the second example the magic
"atomic_int" class performs it's operations using special hardware
instructions so that multiple threads calling f() simultaneously will be
serialized.
Aug 24 '07 #2

P: n/a
Gianni Mariani wrote:
Freedom fighter wrote:
>Hello,

Is a singleton class the same as an atomic class? I know that a
singleton class
can only be instantiated once, but does that concept apply to an
atomic class?

Atomic usually refers to multi-threaded applications. Atomic would mean
that any operation performed on the class would happen in such a way as
the only "visible" states were the state prior to an operation and the
state after the operation was complete. Intermediate states would not
be visible to any other threads.

A classic is an "atomic" int. e.g.

-------------------------------
int val = 0;

int f()
{
return ++ val; // not atomic ...
}
-------------------------------
atomic_int val = 0;

int f()
{
return ++ val; // is atomic
}
-------------------------------

In the first example, "++ val" needs to read, increment and write, not
to mention issues with cache ceherency. In the second example the magic
"atomic_int" class performs it's operations using special hardware
instructions so that multiple threads calling f() simultaneously will be
serialized.
Ah, so that's it then! Thanks so much for that.
Aug 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.