473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

volatile and thread safety

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

Jun 7 '07 #1
3 2719
ar********@gmail.com wrote:
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.
Jun 7 '07 #2
Gianni Mariani wrote:
ar********@gmail.com wrote:
>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.
Where this question has been asked and done to death often, so check the
group's archives first.

--
Ian Collins.
Jun 7 '07 #3
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
Jun 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc Schellens | last post by:
When has a variable to be declared volatile? I have a program wich sets the variable through an interrupt. But its read only from within the same program. What if it would be multi-threaded?...
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
1
by: Eric | last post by:
I have some questions about the volatile keyword... 1) If I use the volatile keyword with a reference type such as a class like so: public volatile UserTotals totals = new UserTotals(); Are...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.