volatile array c++

偶尔善良 提交于 2019-12-10 21:15:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!