3

I have an application that has an array of pointers to MyObject objects:

MyObject **arr;
arr= new MyObject*[10];

The application has two threads, these threads will create and delete new MyObject() to array arr. Therefore arr[n] will be changed all the time, however the MyObject's themselves do not change.

Should I just declare:

volatile MyObject **arr;

Or should I go with:

MyObject ** volatile arr;

Thanks in advance

sigvardsen
  • 1,531
  • 3
  • 26
  • 44
  • Why aren't you declaring the parts that you want not to change as `const`? – John Jul 28 '11 at 20:12
  • 3
    You should probably not use volatile at all. All it does in this case is force the compiler not to optimize on anything that goes near the array, but since you will be needing to use atomic or interlocked operations anyway, that makes no difference... except if you implement a fast forward queue, in which case you need no atomic ops, but `volatile` is greatly detrimental to your performance, depending on the compiler. In any case, it does not add "magical threadsafety". – Damon Jul 28 '11 at 20:13
  • 2
    I concur with 0A0D. Why are you using `volatile`? I think you need to back up and tell us what problem you are trying to solve. Moreover, you should beware that many compilers screw up massively when it comes to `volatile`. – David Hammen Jul 28 '11 at 20:17
  • I might be mistaken but doesn't volatile make sure that when thread 1 is using the variable it will load it from the real memory location, not just some local memory. So that if thread 2 suddenly changes the variable thread 1 will not read the locally stored variable? – sigvardsen Jul 28 '11 at 20:22
  • @sigvardsen: No. Use the tools that do do that. Unix has phtreads; Windows has a thread library as well. And you should beware that even when used per the dicta of the language, compilers screw up massively when it comes to volatile. For example, see http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf . – David Hammen Jul 28 '11 at 20:28
  • @David Hammen: So does that mean that if I use the CreateThread() function provided by Microsoft to create my threads, it will do the job, and I don't have to worry about volatile? – sigvardsen Jul 28 '11 at 20:30
  • You do need to worry very much about thread A wanting to read from some memory location at the same time that thread B wants to write to that location. Use mutexes, read-write locks, and other synchronization schemes, not volatile. Volatile doesn't protect against such events. Volatile exists to support things like memory-mapped I/O devices, not threads. – David Hammen Jul 28 '11 at 20:37
  • @David Hammen: In this artivle http://msdn.microsoft.com/en-us/library/12a04hfd(v=vs.80).aspx they use CreateThread but the variable is still marked as volatile. Further more it states that the "The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a CONCURRENTLY EXECUTING THREAD." – sigvardsen Jul 28 '11 at 20:38
  • @sig: 1) MSDN isn't divine, it contains errors. 2) MSDN is for Visual Studio on Windows. That's a far throw from making claims about `volatile` in C++, in general. 3) So don't be lazy and listen to the experts: [volatile is useless in multithreading](http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/). Period. Sorry, you can't cover your ears and say lalala and say "look it works on some circumstances on this one compiler on this one operating system". It doesn't. Use the proper tools. – GManNickG Jul 28 '11 at 20:43

2 Answers2

4

I think you need MyObject * volatile * arr;.

Please note though that volatile is not an atomic variable or a valid method of synchronization.

Edit: Here it is: http://drdobbs.com/high-performance-computing/212701484

Fozi
  • 4,973
  • 1
  • 32
  • 56
  • I know about the atomic stuff and this is just a "boiled down" version of my code, I do take synchronization into account in the real application – sigvardsen Jul 28 '11 at 20:13
2

I think your use of volatile here is wrong. From Wikipedia,

In C, and consequently C++, the volatile keyword was intended to

  • allow access to memory mapped devices
  • allow uses of variables between setjmp and longjmp
  • allow uses of sig_atomic_t variables in signal handlers.

I noticed this is tagged multi-threading. Intel has a good article on why volatile is mostly useless in multithreading.

Finally, volatile MyObject **arr; is correct syntax - if that is your ultimate intent.