From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.



¿Para Qué Se Utiliza la Palabra Clave "volatile" en LabWindows CVI?



Software Primario:
Versión de Software Primario: 7.0
Versión de Software Primario Corregido: N/A
Software Secundario: N/A

Problema:

Cuando voy a declarar una variable global que utilizaré en mi aplicación multi-hilos de CVI, ¿por qué debo declararla como "volatile"?



Solución:

La declaración como volatile garantiza que cuando el valor de una variable es requerido, dicho valor será el valor más reciente escrito a la variable. Esto es importante en aplicaciones multi-hilo porque potencialmente puede haber muchos hilos acceando una sola variable al mismo tiempo. Algunas veces cuando un hilo cambia el valor de estas variables, los cambios no se actualizan a tiempo antes de que el siguiente hilo accese la variable. Este es el caso en el que la declaración volatile es más útil. Al declarar la variable como volatile garantiza que la variable cambiará inmediatamente. Aquí hay un pequeño código para ilustrar lo anterior:

static volatile int exiting = 0;

int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{

switch (event)
 {
case EVENT_COMMIT:
/* When we quit our program, we first set our exiting variable to 1 */
exiting = 1;
CmtDiscardThreadPool (poolHandle);
QuitUserInterface (0);
break;
}
return 0;

}

/* This is the function that is running in a separate thread */
static int CVICALLBACK MyThreadFunction (void *ctrlID)
{

int threadCount = 0;
/* When the Quit callback function above quits, this "exiting" variable is immediately updated, thereby causing this thread to cease */
while (!exiting)
{
threadCount++;
SetCtrlVal (panelHandle, (int)ctrlID, threadCount);
Delay (0.0001);
}
return 0;

}

 



Ligas Relacionadas:

 

Developer Zone Tutorial: Multithreading in LabWindows/CVI



Archivos Adjuntos:





Día del Reporte: 10/03/2003
Última Actualización: 04/24/2009
Identificación del Documento: 322DK4Y1