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

学习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 phase
send(); 

//completion phase
void sendDone(error_t err) {
  if (err == SUCCESS) {
    sendCount++;
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值