Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于函数延迟处理,在判断延时是否超时时看起来不太理解,特别时计时溢出的情况 #4

Open
jasheri opened this issue Dec 20, 2021 · 1 comment

Comments

@jasheri
Copy link

jasheri commented Dec 20, 2021

void cpostProcess(void)
{
size_t tick;
for (size_t i = 0; i < CPOST_MAX_HANDLER_SIZE; i++)
{
if (cpostHandlers[i].handler)
{
tick = CPOST_GET_TICK();
if (cpostHandlers[i].time == 0 ||
(tick >= cpostHandlers[i].time
? CPOST_GET_TICK() >= cpostHandlers[i].time
: CPOST_MAX_TICK - cpostHandlers[i].time + CPOST_GET_TICK()))
{
cpostHandlers[i].handler(cpostHandlers[i].param);
cpostHandlers[i].handler = NULL;
}
}
}
}

CPOST_MAX_TICK - cpostHandlers[i].time + CPOST_GET_TICK()
这一个判断条件是当 tick<time 时执行,判断是否计时溢出。
这里的话 max >= time 肯定会为真,而只有 max-time=0 且 tick=0 时为假,
这会造成误判吗?
这里不会造成误判的前提是调用此函数的周期必须小于等于systick的周期吗?

@NevermindZZT
Copy link
Owner

这里好像有问题,我验证下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants