volatile and thread safety

A

arun.darra

Hi,

Is the following thread safe??

class Sample
{
private:
volatile int number;
public:
Sample():number(0) {}

void read()
{
++number;
// do nothing
--number;
}
};

I would like to know if the function read() were executed by multiple
threads simultaniously, would function be thread safe?
I mean would the ++ and -- operation be tomic?

Thanks
 
G

Gianni Mariani

Hi,

Is the following thread safe??

class Sample
{
private:
volatile int number;
public:
Sample():number(0) {}

void read()
{
++number;
// do nothing
--number;
}
};

I would like to know if the function read() were executed by multiple
threads simultaniously, would function be thread safe?
NO.

I mean would the ++ and -- operation be tomic?

NO.

Try comp.programming.threads.
 
G

Ge Chunyuan

I would like to know if the function read() were executed by multiple
threads simultaniously, would function be thread safe?

Of course not!
I mean would the ++ and -- operation be tomic?

No, why not try mutex?
there are lots of sample all around

Ge Chunyuan
 

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
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top