Tutorial 3. Binding arguments to a handler

Programming/Boost asio 2015. 3. 15. 16:22

다음에 기반함


http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/tutorial/tuttimer3.html


이제 AsyncTimer 를 수정해서 1초에 한번씩 실행 되도록 수정하겠다고 한다. 


타이머 시간을 수정하고 (기존 5초), 다시 내부에서 새로운 비동기 wait 을 호출하면 된다.

즉, 핸들러내부에서 타이머 객체에 접근하는 것이 필요하다.

2 개의 인자를 추가한다. 


boost::asio::deadline_timer* t (deadline timer 는 reference 가 불가능하게 되어있다.) 와 int* count 이다. (최종 count 값을 리턴받으려면 pointer 로. )


count 는 6번째로 타이머가 실행되었을 때는 타이머를 다시 실행시키지 않도록 하기 위해 추가한 인자다. 

io_service 에는 stop 과 같은 명시적으로 중지를 요청하는 호출이 없고, 대신 'work' 가 없을때 함수가 종료된다는 것을 상기하자.

카운트가 5에 도달하면 새로운 async_wait 을 호출하지 않는 것으로 io_service 는 work 가 소진되고 동작이 중지 될 것이다.


그리고 타이머의 만료시간을 이전 만료시간으로부터 1초 뒤로 지정한다. 이전 시간에서 계산하는 것으로 핸들러를 처리하는데 소요된 시간으로 인해 지연되지 않도록 한다.


periodTimer




메인함수. 호출부



'Programming > Boost asio' 카테고리의 다른 글

Tutorial 5. Synchronising handlers in multithreaded programs  (0) 2015.04.11
Tutorial 4. member function handler  (0) 2015.03.17
Tutorial 2. async_timer  (0) 2015.03.14
Tutorial 1. sync_timer  (0) 2015.02.04
0. 소개  (0) 2015.02.02

Tutorial 2. async_timer

Programming/Boost asio 2015. 3. 14. 16:14

다음 내용에 기반함


http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/tutorial/tuttimer2.html


이전 시간에 deadline_timer 를 이용하여 5초 대기후 메시지를 출력, 종료하는 프로그램을 다뤄봤다. 

sync 함수를 호출했기 때문에 이를 호출한 스레드는 다른 작업을 수행 할 수 없었다.


이제 async 하게 동작하여 호출한 스레드가 다른 작업을 수행할 수 있도록 해보자.


사용했던 함수를 async_wait 으로 변경한것, 그리고 run() 을 호출한 내용이 다르다. 


io_service::run() 은 지정한 work 를 수행하도록 명령한다. 동작이 완료되면 work 에 함께 지정된 핸들러(콜백)를 호출해주는데, 

이때 핸들러는 run() 을 호출한 스레드에서만 호출이 된다. 즉, 스레드풀등에서 run() 을 호출하면 해당 스레드풀에서 핸들러가 호출 되게 하는 방식으로 사용할 수 있다.


수정한 내용이 많지 않기 때문에, 코드를 보면 간단하게 이해가 가능하다.


1. io_service 와 io_object 를 생성하고

2. aync_wait 이라는 work 를 지정하고

3. run() 수행


튜토리얼과 다소 차이가 있는 부분은, 프로젝트의 관리 편의상 클래스를 지정했는데 이에 따라 handler 지정시 bind 로 전달했다.


Class AsyncTimer



int main()



'Programming > Boost asio' 카테고리의 다른 글

Tutorial 5. Synchronising handlers in multithreaded programs  (0) 2015.04.11
Tutorial 4. member function handler  (0) 2015.03.17
Tutorial 3. Binding arguments to a handler  (0) 2015.03.15
Tutorial 1. sync_timer  (0) 2015.02.04
0. 소개  (0) 2015.02.02

bool operator idiom

Programming/C++ 2015. 2. 13. 11:45

http://en.wikibooks.org/wiki/More_C++_Idioms/Safe_bool


흐 - 음. 

선 기록 후 정리


struct Testable
{
    operator bool() const {
          return false;
    }
};
struct AnotherTestable
{
    operator bool() const {
          return true;
    }
};
int main (void)
{
  Testable a;
  AnotherTestable b;
  if (a == b) { /* blah blah blah*/ }
  if (a < 0) { /* blah blah blah*/ }
  // The above comparisons are accidental and are not intended but the compiler happily compiles them.
  return 0;
}


Solution


class Testable {
    bool ok_;
    typedef void (Testable::*bool_type)() const;
    void this_type_does_not_support_comparisons() const {}
  public:
    explicit Testable(bool b=true):ok_(b) {}
 
    operator bool_type() const {
      return ok_ ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }
};
template <typename T>
bool operator!=(const Testable& lhs, const T&) {
    lhs.this_type_does_not_support_comparisons();	
    return false;
}
template <typename T>
bool operator==(const Testable& lhs, const T&) {
    lhs.this_type_does_not_support_comparisons();
    return false;
}
class AnotherTestable ... // Identical to Testable.
{};
int main (void)
{
  Testable t1;
  AnotherTestable t2;
  if (t1) {} // Works as expected
  if (t2 == t1) {} // Fails to compile
  if (t1 < 0) {} // Fails to compile
 
  return 0;
}



In C++11, the explicit keyword can now be applied to conversion operators. As with constructors, it prevents the use of those conversion functions in implicit conversions. However, language contexts that specifically require a boolean value (the conditions of if-statements and loops, as well as operands to the logical operators) count as explicit conversions and can thus use a bool conversion operator.


struct Testable
{
    explicit operator bool() const {
          return false;
    }
};
 
int main()
{
  Testable a, b;
  if (a)      { /*do something*/ }  // this is correct
  if (a == b) { /*do something*/ }  // compiler error
}