Volatile (Informatik)

Volatile (deutsch flüchtig, wechselhaft) ist ein Zusatz bei der Deklaration von Variablen in Programmiersprachen wie C, C++, Java oder C#.

In C und C++ spezifiziert dieser Qualifizierer, dass sich der Wert der Variable jederzeit ohne expliziten Zugriff im Quelltext ändern kann, etwa durch externe Hardware oder asynchron ausgeführte ISRs, und dass alle Zugriffe beobachtbares Verhalten darstellen, also auch Lesezugriffe, deren Wert nicht verwendet wird, sowie wiederholtes Schreiben auf dieselbe Adresse, ohne dass explizit gelesen wird. Das schränkt Compiler bei der Optimierung des Codes ein. Allerdings bezieht sich der Term „beobachtbares Verhalten“ auf das der abstrakten Maschine der Sprachspezifikation,[1] und diese kennt kein Threading.[2] Die Kennzeichnung von Variablen als volatile garantiert also keine Thread-Sicherheit.[3]

Im Gegensatz dazu werden bei Java und C# Variablen mit volatile gekennzeichnet, auf die verschiedene Threads zugreifen, um die Sichtbarkeit zu synchronisieren.[4] Mit volatile gekennzeichnete Variablen werden in Java nicht zwischengespeichert (z. B. in Registern). Dadurch wird sichergestellt, dass auch bei Zugriff von mehreren Threads der richtige Wert gelesen wird. Der Zusatz volatile sorgt in Java nicht für gegenseitigen Ausschluss und ersetzt somit nicht Synchronisationsmechanismen (z. B. Locks oder Semaphore).[5]

Beispiele in C

Ohne volatile könnte der Compiler die Schleife im folgenden Programmausschnitt durch eine einfache Endlosschleife ersetzen und die Variable status wegoptimieren:

static volatile int status;

void poll_status(void)
{
    status = 0;

    while (status == 0) {
        /* do nothing */
    }
}

In folgendem Beispiel könnte ohne volatile ein optimierender Compiler die bedingte Anweisung mit der Grußformel eliminieren.

#include <stdio.h>
#include <setjmp.h>

jmp_buf env;

int main ()
{
   volatile int status = 0;

   if (setjmp (env)) {
      if (status == 1) {
         puts ("Hello World");
      }
      return 0;
   }

   status = 1;
   longjmp (env, 1);
}

Einzelnachweise

  1. British Standards Institute (Hrsg.): The C Standard. Incorporating Technical Corrigendum 1. (Includes the C Rationale. ISO/IEC 9899:1999). John Wiley & Sons, Chichester 2003, ISBN 0-470-84573-2, Kapitel 6.7.3.
  2. Should volatile Acquire Atomicity and Thread Visibility Semantics? In: C++ standards committee. 21. April 2006, abgerufen am 13. März 2018.
  3. Scott Meyers, Andrei Alexandrescu: C++ and the Perils of Double-Checked Locking. Dr. Dobbs Journal, 2004 (pdf).
  4. volatile (C# Reference). Microsoft, abgerufen am 13. März 2018 (englisch).
  5. Christian Ullenboom: Java ist auch eine Insel. Das umfassende Handbuch. (Aktuell zu Java 7). 10., aktualisierte Auflage. Galileo Press, Bonn 2011, ISBN 978-3-8362-1802-3, Kapitel 9.7.1. (openbook.galileodesign.de), abgerufen am 30. Januar 2012.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.