Question d’entretien chez Broadcom

2. volatile variables?

Réponse à la question d'entretien

Utilisateur anonyme

19 sept. 2010

In computer programming, particularly in the C, C++, and C# programming languages, a variable or object declared with the volatile keyword may be modified externally from the declaring object. Variables declared to be volatile will not be optimized by the compiler because the compiler must assume that their values can change at any time [1]. Note that operations on a volatile variable in C and C++ are not guaranteed to be atomic. A volatile variable is modified asynchronously by concurrently running threads. It is not allowed to have a local copy of a variable that is different from the value currently held in “main” memory. Effectively, a variable declared volatile must have it’s data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile variables have a higher access and update overhead than “plain” variables, since the reason threads can have their own copy of data is for better efficiency.

1