Atomic variable for scheduler question
I traditionally use a volatile type for my schedulers. Very simple like so
volatile uint8_t myflag;
uint8_t ticks;
void Func(void){
++ticks;
if(!(ticks % 5)){
myflag = true;
}
}
void User(void){
if(myflag) {
myflag = false;
//DO Something
}
}I'm trying to update my knowledge. The online thought seems to be that this isn't really the safe way as this does not have any memory barrier. So the proper way is to use an atomic. So I tried an atomic.
atomic_bool myflag = ATOMIC_VAR_INIT(false);
uint8_t ticks;
void Func(void){
++ticks;
if(!(ticks % 5)){
atomic_store(&myflag ,true);
}
}
void User(void){
if(atomic_load(&myflag)) {
atomic_store(&myflag ,false);
//DO Something
}
}However, this definitely does not work. The User misses flag settings regularly. Its not just a delay. But per the scope its like a complete flag setting is missed. What have I done wrong here? I even tried to make it volatile and also tried atomic_flag but they both miss settings but the old school way does not miss any settings.
