Mic_chen

It is not the strongest of the species that survive, nor the most intelligent, but the one most responsive to change

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、原子操作问题:

原代码:

int __atomic_add(int *ptr, int value)

{

  int_disable();

  *ptr += value;

    int_enable();

    return *ptr;

}

存在问题:ptr的操作没有在临界区中,return *ptr不是原子操作,可能在返回过程中被中断。

修正后的代码:

int __atomic_add(int *ptr, int value)

{

    int ret;

  int_disable();

  *ptr += value;

    ret = *ptr 

    int_enable();

    return ret;

}

 

posted on 2020-05-14 17:15  Mic_chen  阅读(89)  评论(0编辑  收藏  举报