๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– C๋ž‘ C++

Boost Asio์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž

Boost ๋น„๋™๊ธฐ ํ†ต์‹  ์„œ๋ฒ„&ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ •๋ฆฌ์ค‘...

ํ•„์ˆ˜ ๊ฐœ๋…์ธ Asio์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค ์ƒ๊ฐ๋“ค์–ด์„œ

๋”ฐ๋กœ ๊ฒŒ์‹œ๊ธ€๋กœ ๋บ๋‹ค ใ…ˆใ„ฑใ„ด

 

boost ํ™ˆํŽ˜์ด์ง€์— ์„ค๋ช…์ด ์ž˜ ๋˜์–ด์žˆ์–ด์„œ, ์š”๊ฑธ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค

(์ฒจ๋ถ€ ๋งํฌ ์ฐธ์กฐ)

 


1. Boost.Asio ( Asynchronous I/O ) ๊ฐ€ ๋ญ์ง€?

 

Boost ๊ณต์‹ํ™ˆํ”ผ: Boost.Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach.

 

cf) ์–ด๋–ค ์šด์˜์ฒด์ œ์—ฌ๋„, ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ํ”Œ๋žซํผ์ด ํฌ๋กœ์Šค ํ”Œ๋žซํผ

 

๋จผ์ €, Asio์€ Async I/O์˜ ์ค„์ž„๋ง์ด๊ณ  ๋œป๋Œ€๋กœ ํ•ด์„ํ•˜๋ฉด ๋น„๋™๊ธฐ I/O์ด๋‹ค.

 

์šฐ๋ฆฌ๊ฐ€ Asio ๋ฐฉ์‹์œผ๋กœ ์†Œ์ผ“ ํ†ต์‹ ์„ ๊ตฌํ˜„ํ•  ๋•Œ, 

๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” EPOLL, ์œˆ๋„์šฐ์—์„œ๋Š” IOCP๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ• ํ…๋ฐ

 

์†Œ์ผ“ ํ†ต์‹ ์€ ์ปค๋„๊ณผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‚ฌ์ด๋งŒ ๊ณ ๋ คํ•ด๋„ ๊ตฌํ˜„ํ• ๊ฒŒ ์ฐธ ๋งŽ๋‹ค

 

์†Œ์ผ“ ํ†ต์‹  ๊ด€๋ฆฌ๋„ ํ•ด์ฃผ๊ณ , ์šด์˜์ฒด์ œ ๊ตฌ๋ถ„ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€

boost์—์„œ ๋งŒ๋“  Boost.Asio ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค

 

2. Sync I/O ๊ณผ Async I/O

Boost์—์„œ Asio๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์ด์ „์—,

I/O ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค

 

I/O ๋ฐฉ์‹์€ ๋™๊ธฐ์‹(Sync)๊ณผ ๋น„๋™๊ธฐ์‹(Async) ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค

Blocking๊ณผ Non-blocking ๋น„์Šทํ•œ ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค (๋ฌผ๋ก  ๋‹ค๋ฆ„!)

 

 

๋™๊ธฐ์‹์ด๋ƒ... ๋ธ”๋กํ‚น์ด๋ƒ... ๋น„๋™๊ธฐ๋ƒ.... ๋…ผ๋ธ”๋กํ‚น์ด๋ƒ...

์ด๊ฑฐ๋„ ์•Œ์•„์•ผํ• ๊ฒŒ ๋งŽ์•„์„œ, ๊ธ€์˜ ๋งˆ์ง€๋ง‰์— ์‚ด์ง ์ •๋ฆฌ๋งŒ ํ•ด๋‘๊ฒ ๋‹ค ↓

์šฐ์„  Sync Blocking I/O์™€ Async Non-Blocking I/O์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค๋ฃจ๊ฒ ๋‹ค

 

 

(1) Sync I/O (Blocking์ผ ๋•Œ)

 

Sync I/O + Blocking ์ผ ๋•Œ

I/O๋ฅผ ํ•  ๋•Œ, ์–ดํ”Œ์—์„œ Readํ•˜๊ณ  Write๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ด๋‹ค

 

Sync I/O๋Š” ๋‹จ์ˆœํžˆ ์–ดํ”Œ์—์„œ Read()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ I/O ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ ,

์ปค๋„์ด ๋‹ค ์ฝ์–ด์„œ ์ค„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค

์ปค๋„์€ ๋‹ค ์ฝ์œผ๋ฉด ์–ดํ”Œํ•œํ…Œ ์ฝ์€ Data์™€ Read() ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ค€๋‹ค

 

Sync I/O๋Š” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ , ๋‹จ์ˆœํžˆ ์ปค๋„์˜ ์‘๋‹ต์„ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ผ์„œ,

์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•๋„ ๊ฐ„๋‹จํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค

 

๊ทธ๋Ÿฌ๋‚˜, ๋‹ค ์ฝ์—ˆ๋‹ค๋Š” ์‘๋‹ต์ด ์˜ค๊ธฐ์ „๊นŒ์ง€ ์–ดํ”Œ์€ blocking ๋˜๊ณ , 

๋‹ค๋ฅธ๊ฑด ํ•  ์ˆ˜ ์—†์œผ์…ˆ..

 

EPOLL์„ ์‚ฌ์šฉํ•ด์„œ, socket FD๋ฅผ ๋“ฑ๋กํ•ด์„œ Non-blocking์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Œ

ํ•˜์ง€๋งŒ boost.Asio๋กœ ๊ตฌํ˜„ํ• ๊ฑฐ๋‹ˆ๊นŒ non-blocking์€ ์ƒ๋žตํ•˜๊ฒ ์Œ

 

 

(2) Async I/O (Non-Blocking์ผ ๋•Œ)

 

Async I/O + Non-Blocking์ผ ๋•Œ

 

I/O๋ฅผ ํ•  ๋•Œ, ์–ดํ”Œ์—์„œ Readํ•˜๊ณ  Write๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š”๊ฑด

Sync์ผ ๋•Œ์™€ Async์ผ ๋•Œ ๋ชจ๋‘ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค

 

Async์ผ ๋•Œ ๋‹ค๋ฅธ ์ ์€ ๊ทธ๋Ÿฌ๋ฉด ๋ฌด์—‡์ด๋ƒ..

์–ดํ”Œ์€ Read()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ปค๋„ํ•œํ…Œ ์ฝ์–ด์ฃผ์…ˆ ํ•˜๋ฉด

์ปค๋„์€ ใ…‡ใ…‹ ์ฝ๊ณ  ์•Œ๋ ค์ฃผ๊ฒ ์Œ ํ•˜๊ณ  ์ฟจํ•˜๊ฒŒ ์ฝ์œผ๋Ÿฌ ๊ฐ„๋‹ค

 

Read() ํ˜ธ์ถœ์„ ํ•˜๊ณ , ์ฝ์€ ๊ฒฐ๊ณผ์™€๋Š” ์ƒ๊ด€ ์—†์ด ๊ฒฐ๊ณผ๋ฅผ return ๋ฐ›๋Š”๋‹ค

๊ทธ๋Ÿฌ๋ฉด ์–ดํ”Œ์€ ์ปค๋„์ด ๋‹ค ์ฝ์—ˆ๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ฃผ๊ธฐ ์ „๊นŒ์ง€

๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค

 

์ปค๋„์€ ๋‹ค ์ฝ์œผ๋ฉด, ์ฝ์€ Data๋‚˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์–ดํ”Œ์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š”๋ฐ,

์ด ๋•Œ๋Š” ๋‹ค ์ฝ์—ˆ๋‹ค! ๋ผ๋Š” signal๋งŒ ์ค„ ์ˆ˜๋„ ์žˆ๊ณ , callback ํ˜•ํƒœ๋กœ ์•Œ๋ ค์ค„ ์ˆ˜ ์žˆ๋‹ค

 

์–ดํ”Œ์€ ์ปค๋„์ด ์ฝ์—ˆ๋‹ค๊ณ  ์•Œ๋ ค์ค„ ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ์ผ ํ•˜๋‹ค๊ฐ€

์ฝ์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด, ๋ญ ์ฝ์—ˆ๋‚˜ ๋ณด๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค

์•„์ฃผ ํšจ์œจ์ ์ด๊ตฐ.

 

 

3. Boost.Asio ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

 

Boost.Asio๋Š” ์†Œ์ผ“์ด๋ž‘ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” I/O Object๋ผ๋Š” ๊ฒƒ์œผ๋กœ

Sync์™€ Async I/O๋ฅผ ๋‘˜๋‹ค ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค

 

I/O Object๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ์ฒด์„ ์–ธ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค

 

#include <boost/asio.hpp>

boost::asio::io_context io_context;

 

(io_service ์‚ฌ์šฉ์ด io_context๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค;;;)

ํ”„๋กœ๊ทธ๋žจ์—๋Š” io_context ๊ฐ์ฒด ๊ฐ™์€ I/O ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด์–ธ๊ฐ€๊ฐ€ ์žˆ๊ฒŒ ๋œ๋‹ค

์š”๊ฑธ ์ด์šฉํ•ด์„œ, socket ํ†ต์‹ ๋„ ํ•˜๊ณ ~ ์—๋Ÿฌ ๋ฐœ์ƒํ•˜๋ฉด ์—๋Ÿฌ๋„ ์ฒ˜๋ฆฌํ•˜๊ณ ~ 

 

Boost Asio๊ฐ€ ์›๋ž˜ ์šฐ๋ฆฌ๊ฐ€ ์ปค๋„์„ ํ†ตํ•ด I/O ์ž‘์—…์„ ํ•˜๋˜๊ฑธ ์ค‘๊ฐ„์—์„œ ๋Œ€์‹  ํ•ด์ฃผ๋Š”๊ฑฐ๋ผ๊ณ 

์ƒ๊ฐํ•˜๋ฉด ์ดํ•ด๊ฐ€ ์‰ฝ๊ฒŒ ๊ฐˆ ๊ฒƒ์ด๋‹ค

 

soket FD๋ฅผ ์‚ฌ์šฉํ•ด์„œ Read() Write() ํ•˜๋“ฏ์ด

io_context๋ฅผ ์‚ฌ์šฉํ•ด์„œ Read() Write() ํ•˜๋Š”๊ฑฐ๋‹ค

 

๊ทธ๋Ÿฌ๋ฉด boost์—์„œ io_context๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”์ง€

Sync์™€ Async ๊ตฌํ˜„์„ ๋‚˜๋ˆ ์„œ ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค

(io_service๋ž‘ io_context๊ฐ€ ๊ฐ™์€๊ฑฐ์ž„)

 

(1) Boost.Asio - Sync 

ใ…Žใ…Ž ์ด๊ฒŒ ๊ทธ๋ฆผ์ธ๊ฐ€

TCP ํด๋ผ์ด์–ธํŠธ์ผ ๋•Œ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณด์ž

์œ„์— ๊ทธ๋ฆผ์€ Boost ํ™ˆํŽ˜์ด์ง€์— ์žˆ๋Š”๊ฑด๋ฐ, ์›๋ž˜ ์„ค๋ช…์— ์กฐ๊ธˆ ๋ง๋ถ™์—ฌ์„œ ์ •๋ฆฌํ•˜๊ฒ ๋‹ค

 

์šฐ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์—์„œ io_context ๊ฐ์ฒด๋ฅผ ์ตœ์†Œ ํ•œ๊ฐœ๋Š” ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์€๊ฐ€?

๊ทธ๋Ÿฌ๋ฉด ์š” io_context๋ฅผ ํ†ตํ•ด TCP socket์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค

 

boost::asio::io_context io_context;
boost::asio::ip::tcp::socket socket(io_context);

 

๋ฐ”๋กœ ์š”๋ ‡๊ฒœ. ๊ทธ๋Ÿฌ๋ฉด socket๊ณผ io_context๊ฐ€ ๋“ฑ๋ก(?) ๋๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค

์ด๋ ‡๊ฒŒ ์—ฐ๊ฒฐ๋œ ์†Œ์ผ“์œผ๋กœ I/O operation์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ์‹œ๊ฒ ๋‹ค.

์ดํ›„ ๋™์ž‘์€ ๊ทธ๋ฆผ์— ์žˆ๋Š” ๋ฒˆํ˜ธ์™€ ํ•จ๊ป˜ ์ •๋ฆฌํ•˜๊ฒ ๋‹ค

 

1. ํด๋ผ์ด์–ธํŠธ๋ผ๋ฉด ์„œ๋ฒ„์— ์—ฐ๊ฒฐ์„ ํ•ด์•ผํ•˜์ง€ ์•Š๋Š”๊ฐ€ใ…ใ…

 

tcp::socket socket(io_context);
boost::asio::connect(socket, endpoint);

 

endpoint๋Š” ๋ญ์ง€? ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“คํ…๋ฐ,

๋‹น์—ฐํžˆ connect๋ฅผ ํ•˜๋ ค๋ฉด, ์—ฐ๊ฒฐํ•  ์„œ๋ฒ„์˜ IP ์ฃผ์†Œ์™€ Port ๋ฒˆํ˜ธ๋ฅผ ์•Œ์•„์•ผํ•œ๋‹ค

 

tcp::resolver resolver(io_context);
tcp::resolver::query query("localhost", "7777");
tcp::resolver::iterator endpoint = resolver.resolve(query);

 

endpoint๋Š” resolver๋ผ๋Š” ๊ฒƒ์„ ํ†ตํ•ด์„œ ํ•  ์ˆ˜ ์žˆ๋‹ค

resolver๋Š” ์—ฐ๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ์„œ๋ฒ„์˜ ์ฃผ์†Œ์— ๋Œ€ํ•ด DNS query๋ฅผ ํ•ด์ค€๋‹ค

์ฆ‰, ์–ดํ”Œ์—์„œ DNS ์ด๋ฆ„์„ ํ™•์ธํ•ด์ฃผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค ใ…‹

(์‹ค์ œ ์—ฐ๊ฒฐํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์ฃผ์†Œ์ธ์ง€ ์•„๋‹Œ์ง€)

 

2. io_context๋Š” I/O Execution context์— ์ „๋‹ฌํ•˜๋Š” ์—ญํ• 

์–ดํ”Œ๊ณผ ์ปค๋„ ์‚ฌ์ด์— ์ค‘๊ฐ„ ๋‹ค๋ฆฌ ์—ญํ• ์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ž„

 

3. io_context๋Š” ์ˆ˜ํ–‰ํ•˜๊ณ ์žํ•˜๋Š” I/O ์ž‘์—…์„ ์šด์˜์ฒด์ œํ•œํ…Œ ์•Œ๋ ค์คŒ

 

4. ์šด์˜์ฒด์ œ๋Š” I/O ์ž‘์—…์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ io_context์—๊ฒŒ return ํ•ด์คŒ

 

5. return ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์€ io_context๋Š” ์–ดํ”Œํ•œํ…Œ ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ค˜์•ผ ํ•จ

์–ด๋–ป๊ฒŒ?  boost::system::error_code๋ฅผ ํ†ตํ•ด์„œ

 

6. ๊ฒฐ๊ณผ๋Š” ๋ฐ›๋Š”๊ฑด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ›์Œ

1) error_code๋ฅผ ํ†ตํ•ด์„œ ๋ฐ›๊ธฐ

 

boost::system::error_code ec;
socket.connect(server_endpoint, ec);

 

ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด์„œ ๋ฐ›์•„์˜ค๋ฉด ๋จ ใ…‹

 

2) I/O object๊ฐ€ throws ํ•ด์ฃผ๋Š” exception์œผ๋กœ ๋ฐ›์•„์™€๋„ ๋จใ…‹

 

 

(2) Boost.Asio - Async

๊ทธ๋Ÿฌ๋ฉด ๋น„๋™๊ธฐ์ผ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์งˆ๊นŒ๋ฅผ ๋ณด๊ฒ ๋‹ค

 

 

1. connect๋ฅผ ํ•˜๋Š”๊ฑด ๋˜‘๊ฐ™์€๋ฐ, ํ•จ์ˆ˜์˜ ์ด๋ฆ„๋งŒ ๋‹ฌ๋ผ์ง„๋‹ค

 

socket.async_connect(server_endpoint, your_completion_handler);

 

connect() ๋Œ€์‹  async_connect() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ,

๋น„๋™๊ธฐ ํ†ต์‹ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— callback ํ•จ์ˆ˜๋งŒ ๋“ฑ๋กํ•ด์ฃผ๋ฉด

์‹คํ–‰๊ฒฐ๊ณผ์— ๋Œ€ํ•ด callback์„ ํ†ตํ•ด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

 

2. I/O ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด I/O ์‹คํ–‰ context์—๊ฒŒ ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค ~_~

 

3. I/O ์‹คํ–‰ context๋Š” ์–ดํ”Œ์ด ์š”์ฒญํ•œ Asynchronous connect์— ๋Œ€ํ•ด

์šด์˜์ฒด์ œ์—๊ฒŒ signal์„ ๋ณด๋‚ธ๋‹ค

 

↑ ์—ฌ๊ธฐ๊นŒ์ง€์˜ ๊ณผ์ •์€ ์‚ฌ์‹ค ๋™๊ธฐ์‹ ๋ฐฉ์‹๊ณผ ๋น„์Šทํ•œ๋ฐ,

๋‹ค๋ฅธ๊ฑด ์‹œ๊ฐ„์ด๋‹ค.

์šด์˜์ฒด์ œ์— I/O ์ž‘์—…์ด ์ „๋‹ฌ๋˜๋Š” ๋™์•ˆ์˜ ์‹œ๊ฐ„์€ ๋น„๋™๊ธฐ์‹์—์„œ๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค

 

 

 

4. OS๋Š” I/O ์ž‘์—…์— ๋Œ€ํ•ด ์™„๋ฃŒ๊ฐ€ ๋˜๋ฉด, io_context์—๊ฒŒ ์•Œ๋ ค์คŒ

 

5. ์–ดํ”Œ์—์„œ๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์— ๋Œ€ํ•ด ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ io_context::run()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•จ

 

6. io_context๋Š” ์‹คํ–‰ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด callback ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ๋‹ค 

 

↑ ์œ„์— ๊ทธ๋ฆผ์—์„œ ํ™”์‚ดํ‘œ์™€ ๊ทธ๋ƒฅ ์ ์„ ์ด ์žˆ๋Š”๋ฐ

๋น„๋™๊ธฐ์‹์ด๋‹ˆ๊นŒ ๊ตณ์ด ์ˆœ์„œ๊ฐ€ ์•„๋‹Œ ๋‹จ๊ณ„(?)๋กœ๋งŒ ์ƒ๊ฐํ•˜์ž

 

์‹ค์งˆ์ ์ธ ์ฝ”๋“œ๋Š” ๋‹ค์Œ ๊ธ€์—์„œ.....

 


 

cf) Sync & Async & Blocking & Non-Blocking ์ฐจ์ด์ 

์š”๊ฑด ์˜ค๋Š˜ ์ฃผ์ œ๋ฅผ ์ •๋ฆฌํ•˜๋ฉด์„œ ์•Œ๊ฒŒ๋œ๊ฑด๋ฐ, ์ฒจ๋ถ€๋งํฌ์— ์žˆ๋Š” ์ž๋ฐ”์ง€๊ธฐ ๋‹˜์˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์„œ ์ •๋ฆฌํ–ˆ๋”ฐ

system call์„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด๋ผ ์นญํ•˜๊ฒ ๋‹ค

 

1. Sync & Async ์ฐจ์ด๋Š”?  → ๋ˆ„๊ฐ€ ํ•จ์ˆ˜ ์™„๋ฃŒ ์‹œํ‚ค๋Š”์ง€๊ฐ€ ์ค‘์š”

  • Sync: ์–ดํ”Œ์—์„œ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์˜ ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆผ (ex. return ๊ฒฐ๊ณผ)
  • Async: ์ปค๋„์—์„œ ์–ดํ”Œ์ด ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๋ฅผ ์™„๋ฃŒ์‹œํ‚ค๊ณ , ์–ดํ”Œ์€ ์•ˆ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ๋‹ค๋ฅธ๊ฑฐ ํ•จ
    • (ex. callback ๋“ฑ๋ก ํ›„ ๋‹ค๋ฅธ๊ฑฐ ํ•จ) 

 

2. Blocking & Non-Blocking ์ฐจ์ด๋Š”? → ์–ธ์ œ returnํ•˜๋Š”์ง€๊ฐ€ ์ค‘์š”

  • Blocking: ์–ดํ”Œ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ → ์šด์˜์ฒด์ œ ๋Œ€๊ธฐ ํ์— ๋“ฑ๋ก → ํ•จ์ˆ˜ ์‹คํ–‰ ์™„๋ฃŒ ํ›„ ๊ฒฐ๊ณผ๋ฅผ return ๋ฐ›์Œ
  • Non-Blocking: ์–ดํ”Œ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ → ์‹คํ–‰์™„๋ฃŒ ๋˜๋“  ์•ˆ๋˜๋“  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ return ๋ฐ›์Œ

 

 

3. Sync & Blocking ์ฐจ์ด๋Š”?

  • Sync: ์–ดํ”Œ์—์„œ ํ•จ์ˆ˜ return ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ ๋Œ€๊ธฐ ํ์— ๋จธ๋ฌผ์ง€ ์•Š์•„๋„ ๋จ (ํ•„์ˆ˜X)
    • O_NONBLOCK ์˜ต์…˜ ๋“ฑ์„ ์‚ฌ์šฉํ•ด์„œ Non-blocking์œผ๋กœ ๋ฐ”๋กœ return ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
  • Blocking: ์–ดํ”Œ์—์„œ ํ•จ์ˆ˜ return ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ ๋Œ€๊ธฐ ํ์— ๋จธ๋ฌผ๋Ÿฌ์•ผ ํ•จ (ํ•„์ˆ˜ O)
    • ์™„๋ฃŒ๋˜์–ด์„œ return ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ํ‚ต๊ณ ์ž‰~

 

4. Async & Non-Blocking ์ฐจ์ด๋Š”?

  • Async: ํ•จ์ˆ˜ ์‹คํ–‰์ด ์ž˜ ๋๋‹ค!์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜ ์‹คํ–‰ ํ–ˆ๋‹ค!์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ return
  • Non-Blocking: ํ•จ์ˆ˜ ์‹คํ–‰์ด ์ž˜ ๋๋‹ค! ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด์„œ ์ฝ์„ ์ˆ˜๋Š” ์—†๋‹ค!์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ return

* Boost Asio ์„ค๋ช…

www.boost.org/doc/libs/1_76_0/doc/html/boost_asio/overview/core/basics.html

 

Basic Boost.Asio Anatomy - 1.76.0

Boost.Asio may be used to perform both synchronous and asynchronous operations on I/O objects such as sockets. Before using Boost.Asio it may be useful to get a conceptual picture of the various parts of Boost.Asio, your program, and how they work together

www.boost.org

* Sync&Async I/O ์„ค๋ช…

developer.ibm.com/technologies/linux/articles/l-async/

 

Boost application performance using asynchronous I/O

The most common input/output (I/O) model used in Linux is synchronous I/O. After a request is made in this model, the application blocks until the request is satisfied. This is a great paradigm because the calling application requires no central processing

developer.ibm.com

www.slipp.net/questions/367

 

sync์™€ async, blocking๊ณผ non-blocking ์ฐจ์ด์ ์€?

์˜ค๋Š˜ ์ˆ˜์—… ์‹œ๊ฐ„์— ๋‹ค๋ฃฌ ๋‚ด์šฉ์ด๋‹ค. ๋‚˜๋„ ์ด ์ด ์ฐจ์ด์ ์— ๋Œ€ํ•ด ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค. ๋‚˜๋„ ํ™•์‹คํ•˜์ง€ ์•Š์ง€๋งŒ ํ•™์ƒ๋“ค์„ ๋ฏฟ๊ณ  ๋˜์กŒ๋‹ค. ๊ฐ๊ฐ์˜ ์ฐจ์ด์ ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋˜์ง„ ๋งํฌ๋Š” Boost application performance usin

www.slipp.net

 


์ฝ”๋“œ๋กœ๋งŒ ๊ฐœ๋ฐœํ–ˆ๋˜๊ฑฐ์— ๋Œ€ํ•ด

๊ฐœ๋…์„ ์ดํ•ดํ•˜๋ ค๋‹ˆ๊นŒ ๋†“์นœ ๋ถ€๋ถ„์ด ๋งŽ์•˜๋‹ค 

์ด์ œ๋ผ๋„ ์•Œ๋ฉด ๋ผ