close

C語言中volatile的用法

用法介紹

https://www.embedded.com/electronics-blogs/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword

主要有三種情況下會用到

  • Memory-mapped peripheral registers
  • Global variables modified by an interrupt service routine
  • Global variables within a multi-threaded application

priority inversion簡介

https://www.embedded.com/electronics-blogs/beginner-s-corner/4023947/Introduction-to-Priority-Inversion

因為低優先任務占用資源, 高優先任務無法執行, 卻被不需要此資源的中優先任務搶先執行

此時稱為 priority inversion

解決方法, priority ceiling或 priority inheritance

https://www.embedded.com/design/configurable-systems/4024970/How-to-use-priority-inheritance

priority ceiling 

When a task acquires a shared resource, the task is hoisted (has its priority temporarily raised) to the priority ceiling of that resource.

The priority ceiling must be higher than the highest priority of all tasks that can access the resource.  When the hoisted task releases the

resource, the task is returned to its original priority level.

但是需要把所有task需要的resource都查過, 還要做排序是非常複雜的動作, 所以就有priority inheritance

最糟的情況下priority inheritance需要的時間大於 priority ceiling , 但平均反應時間較優.

 When a low-priority task acquires a shared resource, the task continues running at its original priority level. If a high-priority task requests ownership of the shared resource, the low-priority task is hoisted above the requesting task. The low-priority task can then continue executing its critical section until it releases the resource. Once the resource is released, the task is dropped back to its original low-priority level, permitting the high-priority task to use the resource it has just acquired.

A pointer to constant

int x=4,int y=7;

const int *pt = &x;

cout << *pt;

const int *pt = &y; // Can be assigned to other address

cout << *pt;

*pt=11;  // ERROR!  Can not be assign new value

A Constant pointer

int var1=3, var2 = 5;

int * const cpt = &var1;

* cpt = 8; // Can assign value

cout <<var1;

cpt = & var2; // ERROR! A constant pointer can not be changed

 

 

arrow
arrow
    全站熱搜

    紅線 發表在 痞客邦 留言(0) 人氣()