【问题标题】:Enable Disable interrupt and thread safety启用禁用中断和线程安全
【发布时间】:2017-06-23 06:13:06
【问题描述】:

在获得结果并将 Post_Bits 清除为其原始状态 Post_Bits &= (~Post); 并测试条件之前禁用中断有什么意义,如果之前看到特定的 Post res = Post_Bits & Post;。 例如,如果Post_Bits 用于另一个函数并且您在此处更改它,是否在禁用/启用中断中清除它会使其线程安全?

BOOL Post_a_Note(Post_t Post) // Post_t is a 32bit number
{
    Post_t res; //Final Result
    UINT16 capture = INTDisableInterrupts(); 
    res    =  Post_Bits & Post; 
    Post_Bits &= (~Post); 
    INTRestoreInterrupts(capture); 
    return (res != 0); 
}

【问题讨论】:

  • 因为如果在第 5 行和第 6 行之间发生中断,并设置您正在检查的位,您将清除该位但将其报告为未设置:因此缺少该事件。
  • 中断不是线程,线程安全是另一头野兽。

标签: c thread-safety interrupt


【解决方案1】:

禁用中断是确保代码片段在任何时候仅由一个线程执行的最简单方法。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2014-05-31
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多