使用 void* 来维护状态...(C 编程)
目前我们正在学习如何对 AVR 微控制器进行编程(仅限 Ansi C89 标准)。所包含的驱动程序的一部分是处理调度的标头,即以不同的速率运行任务。我的问题与文档中的引用有关:
“每个任务必须通过使用静态本地来维护自己的状态 变量。”
这到底意味着什么?它们似乎将 void*
传递给函数来维护状态但然后不使用它?
查看文件中的代码,我收集到的意思是:
{.func = led_flash_task, .period = TASK_RATE / LED_TASK_RATE, .data = 0}
/* Last term the pointer term */
有一个函数在数组中使用上述参数运行,但是它仅充当调度程序。 >led_flash_task
是
static void led_flash_task (__unused__ void *data)
{
static uint8_t state = 0;
led_set (LED1, state); /*Not reall important what this task is */
state = !state; /*Turn the LED on or off */
}
And从标题中
#define __unused__ __attribute__ ((unused))
传递 void *data
是为了维护任务的状态,这是什么意思?
谢谢您的帮助?
Currently we are learning how to program AVR micro-controllers (Ansi C89 standard only). Part of the included drivers is a header that deals with scheduling ie running tasks at different rates. My question is to do with a quote from the documentation:
"Each task must maintain its own state, by using static local
variables."
What does that mean really? They seem to pass a void*
to the function to maintain the state but then do not use it?
Looking at the code in the file I gather this is what they mean:
{.func = led_flash_task, .period = TASK_RATE / LED_TASK_RATE, .data = 0}
/* Last term the pointer term */
There is a function that runs with the above parameters in an array however, it only acts as a scheduler. Then the function led_flash_task
is
static void led_flash_task (__unused__ void *data)
{
static uint8_t state = 0;
led_set (LED1, state); /*Not reall important what this task is */
state = !state; /*Turn the LED on or off */
}
And from the header
#define __unused__ __attribute__ ((unused))
And the passing of the void *data
is meant to maintain the state of the task? What is meant by this?
Thank-you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您从 __unused__ 编译器宏中看到的,该参数未使用。通常这样做是因为该方法需要匹配某个签名(中断处理程序、新线程等)。想想 pthread 库的情况,其中签名类似于 void *func(void *data)。您可能会或可能不会使用这些数据,如果您不这样做,编译器会抱怨,因此坚持 __unused__ 宏可以通过告诉编译器您知道自己在做什么来消除警告。
还忘记了静态变量,正如其他答案中所述,静态变量不会在方法调用之间发生变化,因此变量在调用之间保留,从而保留状态(仅在 C++11 中是线程安全的)。
As you can see from the __unused__ compiler macro the parameter is unused. Typically this is done because the method needs to match a certain signature (interrupt handler, new thread, etc...) Think of the case of the pthread library where the signature is something like void *func(void *data). You may or may not use the data and if you don't the compiler complains so sticking the __unused__ macro removes the warning by telling the compiler you know what you're doing.
Also forgot about static variables as was said in the other answer static variables don't change from method call to method call so the variable is preserved between calls therefore preserving state (only thread-safe in C++11).
数据在该函数中未使用(因此是 __unused__)。状态保存在静态变量 state 中,这将在调用之间保留其值。另请参阅 静态变量的生命周期是多少在 C++ 函数中?
data is unused in that function (hence the __unused__). State is kept in the static variable state, which will keep its value between calls. See also What is the lifetime of a static variable in a C++ function?
从文档中:
未使用
这个属性附加到变量上,意味着该变量可能未被使用。 GCC 不会对此变量产生警告。
From the documentation:
unused
This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.
状态必须保存在局部静态变量中。
这意味着在函数内部使用 static 关键字声明的变量:
在您的示例中。
这与传递到任务中的参数无关,在您的示例中不会使用该参数。
The state must be maintained in a local static variable.
That means a variable declared inside the function with the static keyword:
in your example.
This has nothing to do with the parameter passed into the task, which in your example doesn't get used.