Atomic and singleton



 DEVELOP > c-Plus-Plus > Atomic and singleton

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Freedom fighter"
Date: 24 Aug 2007 03:32:29 PM
Object: Atomic and singleton
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.
.

User: "Gianni Mariani"

Title: Re: Atomic and singleton 24 Aug 2007 04:14:50 PM
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.
.
User: "Freedom fighter"

Title: Re: Atomic and singleton 24 Aug 2007 04:33:57 PM
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.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER