Hi, I've been working on a Point class for about 2 weeks now, and I haven't been able to get anywhere.
The specifications are:
+There are two double data members, x and y
+Write overloaded and default constructors
+Write mutator and accessor functions.
+Write an addition function that returns a Point object. Addition is defined such that the x member of the calling Point and the x member of the rhs (right hand side, i.e., the parameter ) Point are added. Return a new point with the added values.
+Write a subtraction formula that returns a Point object. Subtraction is defined such that the x member of the rhs or parameter is subtracted from the x member of the calling Point. So too for the y member. Return a new point with the subtracted values.
+Write a print function that prints a point as follows: ( 2, 3 )
the main is supposed to be something like this:
Point A(1, 2), B(3, 4), C;
cout << "the x value of A is ";
cout << A.GetX( );
cout << endl << "the y value of A is ";
cout << A.GetY( );
cout << endl << "B is";
C = A.AddPoint( B );
cout << endl << "A + B is " ;
C.PrintPoint( ) :
C = A.SubPoint( B );
cout << endl << "A - B is " ;
C.PrintPoint( ) :
cout <<endl << "Set C to (0, 4):";
cout << endl << "C is now " ;
I have written a multithreaded application, and am wondering if I need to declare any of the shared variables as volatile. I have searched online for a while, but have thus far found no definitive source on when you need to use this qualifier. My code operates similarly to the following:
Clearly the const members need not be volatile, and the read/write indices are only read or modified in a single thread, so those are ok. However, do I need to declare the Buffer::data pointer as volatile or not?
I have another multithreaded application that has n > 1 threads, such that each thread modifies some data prior to exiting, but does so within a mutex-protected bit of code. Should this variable be declared as volatile as well? Thank you in advance for any assistance.
There is no special asm emitted for volatile variables (in x86/x64). It is used by the compiler to restrict optimizations and control instruction ordering only. As Stuart mentioned, it isn't a lot of use (by itself) in mt programming.
Normally you'd want a lock, but a quick scan of your code seems to indicate that the algorithm ensures that main() and ThreadFunc() will always be reading/writing different buffer.data indicies - you'd want to make sure this is true, in some sort of formal proof, before letting the code go live.
So, in your case, after ThreadFunc() writes to buffer.data[i] and releases main() to read it, the cpu cache coherency mechanism (x86/x64) will make sure that main() sees the new version of buffer.data[i] if it is running on a different core. You don't need to do anything to make this happen.
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here