volatile

C++ Why does the “const volatile” type qualifier exist? [duplicate]

喜夏-厌秋 提交于 2020-08-08 11:08:16
问题 This question already has an answer here : When are const volatile objects necessary? (1 answer) Closed 4 years ago . Say I have some object declared as const volatile : According to the C++ standard ($7.1.5.1/8): [..] volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation.[...] However, the const qualifier implies than the object is not subject to change, so the

C++ Why does the “const volatile” type qualifier exist? [duplicate]

☆樱花仙子☆ 提交于 2020-08-08 11:07:45
问题 This question already has an answer here : When are const volatile objects necessary? (1 answer) Closed 4 years ago . Say I have some object declared as const volatile : According to the C++ standard ($7.1.5.1/8): [..] volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation.[...] However, the const qualifier implies than the object is not subject to change, so the

Publishing and reading of non-volatile field

大城市里の小女人 提交于 2020-08-06 07:44:46
问题 public class Factory { private Singleton instance; public Singleton getInstance() { Singleton res = instance; if (res == null) { synchronized (this) { res = instance; if (res == null) { res = new Singleton(); instance = res; } } } return res; } } It is almost correct implementation of thread-safe Singleton . The only problem I see is: The thread #1 that is initializing the instance field can published before it will be initialized completely. Now, the second thread can read instance in a

Why is volatile deprecated in C++20?

倾然丶 夕夏残阳落幕 提交于 2020-07-28 14:18:23
问题 According to cppreference, most uses of the volatile keyword are to be deprecated in C++20. What is the disadvantage of volatile ? And what is the alternative solution when not using volatile ? 回答1: There's a good talk by the c++ committee language evolution chair on why. Brief summary, many of the places that volatile is being removed from didn't have any understandable meaning and just caused confusion. 来源: https://stackoverflow.com/questions/59223814/why-is-volatile-deprecated-in-c20

Why is volatile deprecated in C++20?

六眼飞鱼酱① 提交于 2020-07-28 14:15:57
问题 According to cppreference, most uses of the volatile keyword are to be deprecated in C++20. What is the disadvantage of volatile ? And what is the alternative solution when not using volatile ? 回答1: There's a good talk by the c++ committee language evolution chair on why. Brief summary, many of the places that volatile is being removed from didn't have any understandable meaning and just caused confusion. 来源: https://stackoverflow.com/questions/59223814/why-is-volatile-deprecated-in-c20

Intermediate pointers in cast must be “const qualified” - why?

Deadly 提交于 2020-07-08 04:50:39
问题 In the following code... #include <stdlib.h> #include <stdint.h> extern void get_buffer_from_HW_driver(volatile uint32_t **p); void getBuffer(volatile uint32_t **pp) { // Write an address into pp, that is obtained from a driver // The underlying HW will be DMA-ing into this address, // so the data pointed-to by the pointer returned by this // call are volatile. get_buffer_from_HW_driver(pp); } void work() { uint32_t *p = NULL; getBuffer((volatile uint32_t **)&p); } ...the compiler rightfully

Intermediate pointers in cast must be “const qualified” - why?

五迷三道 提交于 2020-07-08 04:50:11
问题 In the following code... #include <stdlib.h> #include <stdint.h> extern void get_buffer_from_HW_driver(volatile uint32_t **p); void getBuffer(volatile uint32_t **pp) { // Write an address into pp, that is obtained from a driver // The underlying HW will be DMA-ing into this address, // so the data pointed-to by the pointer returned by this // call are volatile. get_buffer_from_HW_driver(pp); } void work() { uint32_t *p = NULL; getBuffer((volatile uint32_t **)&p); } ...the compiler rightfully