漂亮的代码,忍不住拿出来

来源:互联网 发布:淘宝网户外用品 编辑:程序博客网 时间:2024/04/27 18:44
学习tinyos lession2中看到的代码。觉得很好,就拿出来大家一起看看哈!主要是用来将task的时间分散开的。

uint32_t i;task void computeTask() {  uint32_t start = i;  for (;i < start + 10000 && i < 400001; i++) {}  if (i >= 400000) {    i = 0;  }  else {    post computeTask();  }}

This code breaks the compute task up into many smaller tasks. Each invocation of computeTask runs through

10,000 iterations of the loop. If it hasn't completed all 400,001 iterations, it reposts itself. Compile this code and

run it; it will run fine on both Telos and mica-family motes.


Note that using a task in this way required including another variable (i) in the component. Because

computeTask() returns after 10,000 iterations, it needs somewhere to store its state for the next invocation. In

this situation, i is acting as a static function variable often does in C. However, as nesC component state is

completely private, using the static keyword to limit naming scope is not as useful. This code, for example, is

equivalent:


task void computeTask() {  static uint32_t i;  uint32_t start = i;  for (;i < start + 10000 && i < 400001; i++) {}  if (i >= 400000) {    i = 0;  }  else {    post computeTask();  }}


下面的这部分则是关于Split-Phase Operations的:

In a blocking system, when a program calls a long-running operation, the call does not return until the

operation is complete: the program blocks. In a split-phase system, when a program calls a long-running

operation, the call returns immediately, and the called abstraction issues a callback when it completes.

This approach is called split-phase because it splits invocation and completion into two separate phases

of execution. Here is a simple example of the difference between the two:

Blocking:if (send() == SUCCESS) {  sendCount++;}Split-Phase:// start phasesend(); //completion phasevoid sendDone(error_t err) {  if (err == SUCCESS) {    sendCount++;  }}


原创粉丝点击