启用禁用中断和线程安全

Enable Disable interrupt and thread safety

在获取结果并将 Post_Bits 清除到其原始状态 Post_Bits &= (~Post); 并测试特定条件 Post res = Post_Bits & Post; 之前禁用中断有什么意义以前见过。 例如,如果 Post_Bits 被用在另一个函数上,而您正在此处更改它,那么在 disabling/Enable 中断中清除它是否使其线程安全?

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); 
}

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