today I knew ABA problem.

http://en.wikipedia.org/wiki/ABA_problem

By the way, suddenly, i just like to know why called "ABA" problem? abbreviation?

有帮助吗?

解决方案

ABA is not an acronym and is a shortcut for stating that a value at a shared location can change from A to B and then back to A :)

其他提示

As far as I know, the problem is related to threads interleaving. So I think it comes as a short textual representation of interleaving. First, run a thread A, then switch to thread B, then get back to thread A.

Assume you have two threads, and one is checking a global character whether there's new data:

char flag = 'n';

void alarms(){
    while(true){
        if(flag == 'f'){
            start_fire_alarm();
        }
        /* ... some other things, including some waiting ...*/
    }
}

void sensors(){
    while(true){
        if(sensor_alerts_fire()){
            flag = 'f';
        } else {
            flag = 'n';
        }
    }
}

Now alarm checks the flag, sees 'n' and everything is fine. Suddenly, a fire starts, and sensors sets the flag to 'f'. But before the operating system gives alarm some time to react, the physical sensors break, and they don't alert the fire anymore. sensors() sets the flag to 'n' again, the operating system gives alarm() some time and nothing happens.

This is the ABA problem (well, in our case NFN). You don't notice in the first thread that your shared value has changed in-between, although this could be critical. Note that you can exchange char with some atomic type and all assignments/tests with atomic ones, the problem would still be the same.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top