Atomic and singleton

F

Freedom fighter

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.
 
G

Gianni Mariani

Freedom said:
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.
 
F

Freedom fighter

Gianni said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top