值的比较不会立即起作用

Comparion of values won't work without delay

我正在编写一个应用程序来计算我们产品的鼠标延迟时间。

它的工作方式是发送鼠标移动,并测量从那时到我们在屏幕上获得像素变化之间的时间。

为什么这个延迟会影响程序。

int check_for_pixel_change() {

    // Gets the pixels R value
    unsigned char value = *((unsigned char *) (0x100));

    // If this delay is not here then the loop will always return 1
    usleep(5);

    if(value == (0x80)) return 0;
    else return 1;
}

int main() {
    // Send move / start timer

    while(check_for_pixel_change());

    // stop timer
    return 0;
}

这是有效的代码。

感谢@barak-manos && @santosh-a。

我需要将 char 更改为 volatile,因为它会被不同的程序不断更改。

int check_for_pixel_change() {

    // Gets the pixels R value
    volatile unsigned char value = *(volatile unsigned char *) (0x100);

    if(value == (0x80)) return 0;
    else return 1;
}

int main() {
    // Send move / start timer

    while(check_for_pixel_change());

    // stop timer
    return 0;
}