NS3 Timer類說明

官方文檔介紹

一個簡單的Timer類。
定時器用于將延遲保持在一起,延遲到期時調用的函數以及延遲到期時傳遞給函數的一組參數。
定時器可以暫停,恢復,取消并查詢剩余時間,但不能擴展(除了暫停和恢復)。
也可以使用定時器來執行一組預定義的事件生命周期管理策略。 這些政策在構造時規定,以后不能改變。

Timer類似于Simulator::Schedule完成的功能,并且,Timer的本質也是使用的就是Simulator::Schedule,只不過Timer對它做了一層封裝,使用起來更加方便、明了。但是如果說代碼簡潔的角度來說的話,Simulator::Schedule代碼更簡潔。

Timer的簡單使用

Timer m_nudTimer (Timer::CANCEL_ON_DESTROY);
if (m_nudTimer.IsRunning ())
    {
      m_nudTimer.Cancel ();
    }

  m_lastReachabilityConfirmation = Simulator::Now ();
  //設置回調函數
  m_nudTimer.SetFunction (Function);
  //設置時延
  m_nudTimer.SetDelay (MilliSeconds (delay));
  //啟動調度
  m_nudTimer.Schedule ();

上面的代碼與下面的代碼等同:

Simulator::Schedule (MilliSeconds (delay), Function);

也可以設置一些參數:

Timer m_nudTimer (Timer::CANCEL_ON_DESTROY);
if (m_nudTimer.IsRunning ())
    {
      m_nudTimer.Cancel ();
    }

  m_lastReachabilityConfirmation = Simulator::Now ();
  //設置回調函數, 函數屬于object對象
  m_nudTimer.SetFunction (&Function, object);
  //設置參數
  m_nudTimer.SetArguments(a1, a2, a3);
  //設置時延
  m_nudTimer.SetDelay (MilliSeconds (delay));
  //啟動調度
  m_nudTimer.Schedule ();

上面的代碼與下面的代碼等同:

Simulator::Schedule (MilliSeconds (delay), &Function, object, a1, a2, a3);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容