|  | 
 
| Split-Phase操作 
 比较以下Blocking和Split-Phase操作的两段代码
 Blocking
 
 if (send() == SUCCESS) {
 sendCount++;
 }
 在Block的系统中,当要调用一个运行时间很长的操作时,直到操作完成调用才能返回。例如,只有执行完send()才会有返回值。
 
 Split-Phase
 // start phase
 send();
 
 //completion phase
 void sendDone(error_t err) {
 if (err == SUCCESS) {
 sendCount++;
 }
 }
 在Split-Phase操作中,将程序的执行分为两个部分,开始部分和完成部分。
 Split-Phase操作的好处:
 1.节省内存,与block的操作需要stack相比,节省了内存stack的开销。
 2.可以并行运行多个操作。
 下面是另一个例子
 Blocking:
 
 state = WAITING;
 op1();
 sleep(500);
 op2();
 state = RUNNING
 
 在此个Block的例子中,整个程序需要进行sleep(500)
 
 Split-phase:
 
 state = WAITING;
 op1();
 call Timer.startOneShot(500);
 
 event void Timer.fired() {
 op2();
 state = RUNNING;
 }
 此Split-Phase的操作,在调用Timer.startOneShot(500)后,程序立即返回,整个程序并没有停止,过500ms后,自动调用event void Timer.fired()
 
 
 | 
 |