问题
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
回答1:
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
回答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.
来源:https://stackoverflow.com/questions/6865001/volatile-array-c