Boost C++ Libraries/Boost.Asio

Boost.Asio 색인 - deadline_timer

까마귀75 2021. 1. 19. 23:08
728x90
반응형

deadline_timer

타이머의 일반적인 사용법에 대한 typedef 이다. UTC 타임을 사용한다.

typedef basic_deadline_timer< boost::posix_time::ptime > deadline_timer;

유형

이 름 설 명
rebind_executor 타이머 유형을 다른 실행기(executor)로 리바인드한다.
duration_type 기간 유형이다.
executor_type 개체와 연결된 실행기(executor) 유형이다.
time_type 시간 유형이다.
traits_type 시간 특성 유형이다.

멤버 함수

이 름 설 명
async_wait 타이머에서 비동기 대기를 시작한다.
basic_deadline_timer
[constructor]
생성자(constructor)

특정 만료 시간을 절대 시간으로 설정하는 생성자(constructor)

특정 만료 시간을 상대 시간으로 설정하는 생성자(constructor)

다른 basic_deadline_time에서 이동 생성하는 이동-생성자(move constructor)
cancel 타이머에서 대기중인 모든 비동기 작업을 취소한다.
cancel_one 타이머에서 대기중인 하나의 비동기 작업을 취소한다.
expires_at 타이머의 만료 시간을 절대 시간으로 가져온다.

타이머의 만료 시간을 절대 시간으로 설정한다.
expires_from_now 타이머의 만료 시간을 상대 시간으로 가져온다.

타이머의 만료 시간을 상대 시간으로 설정한다.
get_executor 개체와 연결된 실행기(executor)를 가져온다.
operator= 다른 basic_deadline_timer에서 이동 할당한다.
wait 타이머에서 블럭 대기를 수행한다.
~basic_deadline_timer
[destructor]
타이머를 소멸한다.


basic_deadline_timer 클래스 템플릿은 타이머가 만료될 때까지 블럭 또는 비동기 대기를 수행하는 기능을 제공한다.

데드라인 타이머는 항상 "expired(만료됨)" 또는 "not expired(만료되지 않음)"의 두 가지 상태중 하나이다. 만료된 타이머에서 wait() 또는 async_wait() 함수가 호출되면 대기 작업이 즉시 완료된다.

대부분의 응용프로그램은 deadline_timer typedef를 사용한다.

스레드 안전성

고유 개체 : 안전함
공유 개체 : 안전하지 않음

예제

블럭 대기 수행:

// Construct a timer without setting an expiry time.
boost::asio::deadline_timer timer(my_context);

// Set an expiry time relative to now.
timer.expires_from_now(boost::posix_time::seconds(5));

// Wait for the timer to expire.
timer.wait();


비동기 대기 수행:

void handler(const boost::system::error_code& error)
{
  if (!error)
  {
    // Timer expired.
  }
}

...

// Construct a timer with an absolute expiry time.
boost::asio::deadline_timer timer(my_context,
    boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));

// Start an asynchronous wait.
timer.async_wait(handler);

사용중인 deadline_timer의 만료 시간 변경

대기중인 비동기 대기가 있는 동안 타이머의 만료 시간을 변경하면 해당 대기 작업이 취소된다. 타이머와 연관된 작업이 한번만 수행되도록 하려면 다음과 같이 사용한다:

void on_some_event()
{
  if (my_timer.expires_from_now(seconds(5)) > 0)
  {
    // We managed to cancel the timer. Start new asynchronous wait.
    my_timer.async_wait(on_timeout);
  }
  else
  {
    // Too late, timer has already expired!
  }
}

void on_timeout(const boost::system::error_code& e)
{
  if (e != boost::asio::error::operation_aborted)
  {
    // Timer was not cancelled, take necessary action.
  }
}
  • boost::asio::basic_deadline_timer::expires_from_now() 함수는 보류중인 비동기 대기를 취소하고 취소된 비동기 대기수를 반환한다. 0을 반환하면 작업이 너무 늦은 것으로 대기 핸들러가 이미 실행되었거나 곧 실행될 것이다. 1을 반환하면 대리 핸들러가 성공적으로 취소된 것이다.
  • 대기 핸들러가 취소되면 전달된 boost::system::error에는 boost::asio::error::operation_aborted 값이 포함된다.

요구 사항

일반 헤더: boost/asio/deadline_timer.hpp
편의 헤더: boost/asio.hpp

Boost.Asio 홈

728x90
반응형