当两个变量之间可能发生中断时,可以安全地访问它们 [英] Access to two variables safely when an interrupt might occur between them

查看:103
本文介绍了当两个变量之间可能发生中断时,可以安全地访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我欢迎对这个问题的标题进行编辑,我想不出如何更好地措辞,但是我对自己的想法并不满意.

这是一个关于并发性的问题,我的应用程序是用C语言编写的微控制器,但是我认为这没什么大不了的.

我有一个中断例程,可以更改两个全局变量的值.我有一些可以读取这些变量的主要代码.但是它必须从两个值中获取一致的值,这意味着我无法读取一个值然后再读取另一个值,因为可能会在两者之间发生中断并同时更改它们,从而使我只能从一组中读取一个,而从另一组中读取. /p>

通常,我只会对读取这两个变量的一小部分代码禁用中断,但是我不能这样做,因为需要在恰好正确的时间调用该中断,而调用中不应出现抖动".读取和存储变量的4或5条指令将导致中断时序中的过多抖动. (我在中断中生成PAL视频,因此任何时间上的抖动都会导致屏幕上像素的可见运动).

我可以用不同的方式重新安排代码来执行此操作,以使同一中断不负责这两件事,从长远来看,我可能会这样做,但这是非常重要的工作.无论如何,这个问题现在已经成为我的一个难题",即使我稍后重新编码以避免这种情况.

所以我要问的是,有什么办法可以读取两个变量并确保它们在不禁用中断的情况下处于一致状态吗?

只要两个值都来自同一位置,我获取中断发生之前还是之后的值都没关系.

我已经考虑过要读取一个单独的版本号"计数,以确保每个变量都是所读取的相同版本,但这使问题变得更糟,因为现在我要读取4个变量.

解决方案

您正在使用哪种微控制器.我们在谈论什么样的变量?如果需要少于4个字节并使用32位MCU,则可以通过将两个变量放入一个32位变量来解决此问题.

如果您使用的是8位MCU,我认为最好的方法是将变量放入结构中,创建此结构的数组(2个元素),并使用一个变量指示当前用于读取的结构.然后,您的中断将更改未使用的结构,然后更改指标的值.

First of all I'd welcome edits to the title of this question, I couldn't think how to word it better but I'm not too happy with what I came up with.

This is a question about concurrency, my application is on a microcontroller in C but I don't think that matters a great deal.

I have an interrupt routine which can change the values of two global variables. I have some main code which can read those variables. But it must get get consistent values from both which means I can't read one and then read the other because the interrupt might occur between the two and change them both, leaving me having read one from one set and one from the other.

Normally I would just disable the interrupt for the tiny part of code that reads both variables but I can't do this because the interrupt needs to be called at exactly the right time with no "jitter" in calls. 4 or 5 instructions to read and store the variables will lead to too much jitter in interrupt timing. (I'm generating PAL video in the interrupt so any jitter in timing will lead to visible movement of pixels on the screen).

I could rearrange the code to do this in a different way so that the same interrupt isn't responsible for the two things, and long term I'll probably do this, but it's significant work to do so. The question has become interesting to me anyway now as a 'puzzle' even if I recode it later to avoid the situation.

So what I'm asking is, is there any way I can read both variables and ensure they are in a consistent state without disabling interrupts?

It doesn't matter if I get the value before or after the interrupt occurs as long as both values come from the same place.

I've thought about having a separate "version number" count to read to ensure that each variable is the same version that is read, but that just makes the problem worse as now I have 4 variables to read.

解决方案

What microcontroller are you using. What kind of variables are we talking about? If you need less then 4 bytes and using 32bit MCU, you can solve this problem by putting the two variables into one 32bit variable.

EDIT:

If you are using 8 bit MCU, I think that the best way to you can do is to put variables to structure, create array of this structures (2 elements) and have one variable which indicates which structure is currently used for read. Then your interrupt changes the unused structure, and after that changes the value of indicator.

这篇关于当两个变量之间可能发生中断时,可以安全地访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆