假设函数 isr_callback() 在硬件中断上被调用。如果 my_function() 设置变量 data 为 0,并等待 tx_complete_semaphore,那么当 tx_complete_semaphore 被 isr_callback() 释放时,变量 data 是否会在 my_function() 中更新为 1?或者是否必须将变量数据限定为 volatile 才能在 my_function() 中正确更新?
static int data;
static rtems_id tx_complete_semaphore;
void isr_callback(void)
{
data = 1;
/* interrupts as disabled here */
rtems_semaphore_release(tx_complete_semaphore);
}
void my_function(void)
{
data = 0;
/* data will be 0 here */
printf("data is %i", data)
/* Interrupts are enabled here */
rtems_semaphore_obtain(tx_complete_semaphore,
RTEMS_WAIT,
RTEMS_NO_TIMEOUT);
/* what is the value of data here? */
printf("data is %i", data);
}