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

Tutorial 1. sync_timer

Programming/Boost asio 2015. 2. 4. 22:58

다음 내용에 기반합니다. 


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


시작하기에 앞서, 환경을 구성한다.

boost windows prebuilt 를 받아 적당한 경로에 준비시켜 두었으며,

그 적당한 경로를 Project > Properties 에서 include header 설정 해두었고, 



마찬가지로 lib 파일의 경로도 잡아준다. 




lib 파일의 경로는 잡아줬지만, 실제 링킹되어야 하는 파일명은 dependencies 에 지정하지 않은 상태다.

직접 링크에러가 날때마다 properties 에 추가하거나, pragma comment 로 소스코드에서 linking 할 수 있는데, 

편의상 소스코드에 넣었다.


sync_timer 소스코드는 다음과 같다. 



첫 튜토리얼 답게 굉장히.. 간단하다.

그냥 asio 의 deadline_timer 를 이용, 생성시 지정한 시간만큼 대기하다 종료하는 프로그램.

deadline_timer 의 wait() 함수는 지정된 시간이 지나기 전까지 리턴되지 않는다.


io_service 라는 생소한 클래스와 deadline_timer 라는 녀석이 있는데, 

deadline_timer 는 그냥 timer 기능을 제공하는 녀석이구나 정도로 쉽게 이해가 갈텐데, 

이 녀석이 사용하는 io_service 라는 클래스가 앞으로도 상당히 자주 보게될 클래스. 

I/O 기능을 제공한다 - 라고 튜토리얼에서는 소개하고 있는데, 단독으로 사용되는 경우는 없고,(아는한) 이 예의 deadline_timer 같은 io_object 와 결합이 되어 사용된다. 


IO_SERVICE 

> io_object 로부터 시작 요청을 받으면 OS 와 io 동작을 제공해주는 클래스. 

sync 의 경우에,

시작함수 (initiating function) 가 호출된 스레드에서 I/O 를 수행한 후 리턴


async 의 경우에, 

io_object 의 시작 함수가 (initiating function) 호출되면 io_object 에 지정된 io_service 가 OS 영역-정확히는 아니지만 이렇게 이해해도 큰 무리가 없다고 boost 개발자가 설명한바있다..(로 들렸다)-에 work 라는 객체와 핸들러를 함께 전달하여 queuing 해뒀다가, io_service.run() 이 호출되면 OS 로 실제로 요청 동작을 실행하고, 

OS 로부터 io_service 로 완료 알림이 오면 io_service 는 생성했던 work 객체와 handler 를 dequeue 하여 핸들러를 호출한다. 


실행결과는 - 지정한 5초동안 아무 반응이없다가 hello world 가 출력되고 프로그램이 종료되는 것이다.





'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 2. async_timer  (0) 2015.03.14
0. 소개  (0) 2015.02.02

0. 소개

Programming/Boost asio 2015. 2. 2. 00:22

C++ Third-party 라이브러리 (라기엔 너무 메이저한) Boost 의 부분인 

asio 의 tutorial 을 정리할 예정입니다. 


다음 내용에 기반합니다.


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

Basic Skills

The tutorial programs in this first section introduce the fundamental concepts required to use the asio toolkit. Before plunging into the complex world of network programming, these tutorial programs illustrate the basic skills using simple asynchronous timers.

Introduction to Sockets

The tutorial programs in this section show how to use asio to develop simple client and server programs. These tutorial programs are based around the daytime protocol, which supports both TCP and UDP.

The first three tutorial programs implement the daytime protocol using TCP.

The next three tutorial programs implement the daytime protocol using UDP.

The last tutorial program in this section demonstrates how asio allows the TCP and UDP servers to be easily combined into a single program.