νΉν μκ°μμ΄ μ μ§λ₯΄λ μ€μκ° μλμκ² μ΄λ€ μμ²λ₯Ό μ£Όλμ§ μμΌ λ°κ²¬νκ² λλ€. - "10μΌμμ λ¨μμΉκ΅¬μκ² μ°¨μ΄λ λ²" νλ‘ μμ.
λͺ©μ°¨ #
2 μμνλ©° #
- λ§μ κ³³μμ λ€νΈμν¬ μλ²μ λν λ΄μ©μ΄ λμ€μ§λ§, Completion Portsμ λν΄ μμΈνκ² μ€λͺ ν κ³³μ λ³λ‘ μμλ κ² κ°μ΅λλ€. μ μμ λ λ νμΌλ‘ μ»μ΄λΈκ±°λΌ μ½κ°μ μ μμ μ΄μ§ μκ³ νΈλ²μ μΈ λ΄μ©λ μμ μ μμ΅λλ€.
- μ°Ύμμ΅λλ€. CPμ κ΄λ ¨λ μν°ν΄μ΄ μλκ΅°μ. μ¬κΈ°λ₯Ό κ°λ³΄μκΈ°λ°λλλ€.
http://www.codeproject.com/internet/iocp.asp
http://www.codeproject.com/internet/winsockiocp.asp
- μ΄λ€ κ³³μμλ Completion Portλ₯Ό CPIOλΌκ³ λ νκ³ IOCPλΌκ³ λ ν©λλ€. μ¬κΈ°μλ κ·Έλ₯ CPλΌκ³ νλλ‘ νκ² μ΅λλ€.
- Overlapped I/Oλ OLμ΄λΌκ³ μ€μ¬μ μ°κ² μ΅λλ€. (HTMLνκ·Έκ° μλλλ€.
) μΈμ λμ²λΌ μ‘΄μΉμ μλ΅νκ² μ΅λλ€.
3 CPκ° λμ§? #
- λ³΄ν΅ Win32 μ΄μ체κ³μμμ λΉλκΈ°μ μ
μΆλ ₯μ μ§μνκΈ° μν΄ μ¬μ©λλ μ₯μΉλΌλ©΄ OLκ³Ό CPμ λ€ μ μμ΅λλ€. μ΄λ λ€μμ λκ°μ§ λͺ©μ μ μν΄ μ¬μ©λλ€κ³ λ³Όμ μμ£ .
- λΈλ‘νμ§ μκ³ μ μΆλ ₯μ μ§μν μ μλ€. - μ€λ Ή νμ¬ μμ€λΌμΈμμ μ μΆλ ₯μ΄ μλ£λμ§ μμλ€κ³ νλλΌλ μΌλ¨μ λ€μ μμ€λΌμΈμ λͺ λ Ήμ μννκ³ μνλ λΆλΆμμ μ±ν¬ν μ μλ€.
- μ μΆλ ₯ μ’ λ£μ§ν μ΄λ€ μ½λλ₯Ό μ§μ ν μ μλ€. - μ΄κ²μ νΈλ¦κ³Ό μ머리λ₯Ό λ§λ€μ μλ μμκ° λλ€. μλ₯Ό λ€μ΄ μ½κΈ° μμ μ΄ λλλ©΄ λ°λ‘ λ€μ μ½κΈ°λ₯Ό μ€νν μ μλ€.
- CPλ κ°λ¨ν λ§ν΄μ "μ΄λ€ μ μΆλ ₯μ μ’ λ£μ¬λΆλ₯Ό κ³μ λ³΄κ³ μλ€κ° μ’ λ£μμ μμ μ§μ λ μ²λ¦¬λ₯Ό λ°±κ·ΈλΌμ΄λμμ μ€νν΄μ£Όλ μ₯μΉ"μ΄λ€. μ¦, λ°±κ·ΈλΌμ΄λ μ€νμ΄ κ°λ₯νκΈ°μν΄ μ°λ λκ° νμνλ€.
- 'μ΄? 그건 OLμ΄μλ!'λΌκ³ ν μλ μκ² λ€. κ°λ μ μΈ μΈ‘λ©΄μμλ λ§λ€. CPλ μ μΆλ ₯μ²λ¦¬λ₯Ό μν΄μλ OLκ³Ό κ°μ΄ μ¬μ©ν΄μΌνκΈ° λλ¬Έμ μ΄λ»κ² 보면 OLμ μ’ λ£μ±ν¬λ°©λ² μ€ νλλΌκ³ μκΈ°ν΄λ λ§λ€κ³ ν μ μλ€. (OLμ λν λ΄μ©μ Overlapped I/O μ΄μΌκΈ° μ°Έμ‘°)
- λ³΄ν΅ CPμ μλ ₯μ΄ λ°νλλ λΆλΆμ μλΉν λ§μ μ¬μ©μκ° λμμ μκ°λ₯ν μλ²λ₯Ό μ μν λμ΄λ€. μμΌλ νμΌμ μΌμ’ μ΄λΌλ κ²μ μλ§ μμ리λΌκ³ λ―Ώλλ€. OLμ체λ§μΌλ‘λ μ¬λ¬κ°μ μ μΆλ ₯μ λμμ κ΄λ¦¬νκΈ°λ μ½μ§ μλ€. WSAWaitForMultipleEvents()ν¨μλ₯Ό μ¬μ©ν΄μ μ΄λ²€νΈ κ°μ²΄λ₯Ό 체ν¬ν΄λ 64κ°κ° νκ³λκΉ 64κ° μ΄μμ μ μμ λ§λλ κ²μ λ¬΄λ¦¬κ° μλ€. κ²λ€κ° κ·Έκ²λ€μ λΉλκΈ°μ μΌλ‘ 체ν¬νλ €λ©΄ κ°κ°μ μ²λ¦¬λ₯Ό μν μ°λ λλ₯Ό κ΄λ¦¬ν΄μΌνλ λΆλ΄λ μκΈ΄λ€.
- CPλ CPUμ±λ₯μ΄ νλ½νλν νλ‘κ·Έλλ¨Έκ° μ§μ ν μμ μ μΆλ ₯μ λ°μν μμλλ‘ μ²λ¦¬νλ μν μ νλ€. μ¦, λͺ κ° μλλ μμΌμ²λ¦¬λ₯Ό μν΄μ CPλ₯Ό μ΄λ€λ κ²μ κ°λ¨ν λ°©λ²μΌλ‘ μ²λ¦¬κ° κ°λ₯ν λ°λ λΆκ΅¬νκ³ λ³΅μ‘ν λ°©λ²μ ννλ κ²½μ°κ° λ μ μλ€. (CPλ ꡬ쑰μ ν¨μλ λͺ κ° μλμ§λ§ μ μΆλ ₯ ν¨μμ μμ€μ μμΉλ₯Ό κΈ°μ μΌλ‘ μ€λΉλΆλΆκ³Ό μλ£λΆλΆμ΄ λλμ΄μ§κΈ° λλ¬Έμ μ λ°μ μΌλ‘ μ΄ν리μΌμ΄μ μ κ΅¬μ‘°κ° λ³΅μ‘ν΄μ§ μ μλ κ²μ΄ λ¨μ μ΄λ€. μλ§λ μλμ°μ¦ μ΄μμ²΄κ³ μμμλ μμΌκ³Ό κ²°ν©νλ € μ μν μ μλ κ°μ₯ 볡μ‘ν λͺ¨λΈμ΄λΌκ³ νκ² λ€.)
- ACE λΌμ΄λΈλ¬λ¦¬μμλ proactorλΌλ ν¨ν΄μΌλ‘ ꡬνμ΄ λμ΄μλ€. ACE νλ‘κ·Έλλ° μ°Έμ‘°.
4 CPμ μμΌκ³Όμ μκ΄κ΄κ³ #
μ κ·Έλ¦Όμ CPμ λ΄λΆ μ²λ¦¬ ꡬ쑰λ₯Ό κ°λ¨νκ² κ·Έλ €λ³Έ κ²μ΄λ€. (μ~ μ λ§ μ°λ νλ€.
) μμμ Socketμ΄λΌκ³ λμ΄μλ λΆλΆμ μ΄λ―Έ CPμ λ±λ‘λμ΄μλ μμΌμ λ§νλ€. μ¦, CPμμ μμΌμ μ
μΆλ ₯μ νλ¨νλ μμλ λ€μκ³Ό κ°λ€.
) μμμ Socketμ΄λΌκ³ λμ΄μλ λΆλΆμ μ΄λ―Έ CPμ λ±λ‘λμ΄μλ μμΌμ λ§νλ€. μ¦, CPμμ μμΌμ μ
μΆλ ₯μ νλ¨νλ μμλ λ€μκ³Ό κ°λ€.
- μ°μ μ μΆλ ₯μ΄ λ°μν μμΌμ CPμ λ±λ‘νλ€. (μλ "CPμ μμΌν λΉνκΈ°"μ°Έμ‘°)
- μμΌμ μ¬μ©νμ¬ WSARecv/WSASendλ₯Ό μ€ννλ€.
- ν΄λΉ
5 κ΄λ ¨ ν¨μ μ¬μ©λ² #
- μ°μ MSDNμμ 무μ§νκ² λ°°κΌμμ λ°νλ€. CPμ κ΄λ ¨λ ν¨μλ λ¬λ 3κ°μ΄λ€.
HANDLE CreateIoCompletionPort ( HANDLE FileHandle, // νμΌνΈλ€. λ€νΈμν¬μ©λλΌλ©΄ μμΌνΈλ€μ λ£μΌλ©΄ λλ€. HANDLE ExistingCompletionPort, // CPνΈλ€ ULONG_PTR CompletionKey, // μλ£ ν€ κ΅¬μ‘°μ²΄. λμ€μ μ€λͺ . DWORD NumberOfConcurrentThreads // λμμ μ€νλ μ°λ λμ μ );
μ΄ ν¨μλ νΉλ³νκ² 2κ°μ§ μν μ νλ€. κ·Έ μν μ κ°λ¨ν μμ½νλ©΄ λ€μκ³Ό κ°λ€.
- CP μ체μ νΈλ€ μμ±(μ΄κΈ°ν)
- μ
μΆλ ₯μ΄ λ°μνλ νΈλ€λ±λ‘ - (μ¬κΈ°μλ μμΌμ΄ λκ² λ€)
5.1 CP νΈλ€ μ»κΈ° #
λ€μκ³Ό κ°μ΄ μ¬μ©νμ¬ CPνΈλ€μ μ»μ μ μλ€. λ¬Όλ‘ CreateIoCompletionPortλ₯Ό μ¬μ©νλ€.
HANDLE hCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, NULL, NumThreads);
μμμ hCPλ CPμ νΈλ€μ λ΄μ λ³μλ₯Ό λνλ΄κ³ , NumThreadsλ CPμ κ΄λ ¨λμ΄μ Έμ μ€νλλ "μΌκΎΌ μ°λ λ"μ κ°μλ₯Ό μλ―Ένλ€. μ΄ κ°μμ κ°μ₯ ν¨μ¨μ μΈ μμΉλ νμ¬ μμ€ν
μ μ€μΉλ CPUμ μμ΄μ§λ§ λ§μΌ μ°λ λλ΄μμ μκ°μ μΌλ‘ μ²λ¦¬ν΄μΌν μκ°μ΄ κΈΈμ΄μ§λ€λ©΄ μμμ κ°μλ‘ μ ν μ μλ€. νμ§λ§, CPμ λ΄λΆμ μΈ νΉμ±μ μ¬κΈ°μ μ§μ ν κ°μμ μ€μ μμ±ν μΌκΎΌ μ°λ λμ μλ μ νν λ§μλ¨μ΄μ ΈμΌ νλ€.
λ§μΌ NumThreadsλ₯Ό 0μΌλ‘ μ€μ νλ©΄ νμ¬ μμ€ν
μ μ€μΉλ CPUμ μλ‘ μλμΌλ‘ μ€μ λλ€.
5.2 CPμ μμΌν λΉνκΈ° #
Acceptλμ΄ μμΌλ‘ κ³μ Send/Recvκ° λ°μν μμΌ νΈλ€μ connectsockμ΄λΌκ³ νλ©΄,
if (CreateIoCompletionPort((HANDLE)connectsock, hCP, (DWORD)tCPKey, 0) == NULL)
{
// CP μμ±μλ¬. μ΄ μμ μμ CPλ λ±λ‘λμ΄μμ§ μλ€.
}
λ€μκ³Ό κ°μ΄ μ¬μ©νκ² λλ€. (맨λ§μ§λ§ μΈμμ λ΄μ©μ΄ 0μΈ κ²μ μ£Όλͺ©ν΄λΌ. λ§μ§λ§μΈμλ μμΌμ λ±λ‘νλλ° μμ΄μλ μ¬μ©λμ§ μλλ€.)
5.3 μΌκΎΌμ°λλ λ΄λΆκ΅¬μ‘° #
(보좩μλ§)
BOOL GetQueuedCompletionStatus( HANDLE CompletionPort, // CPνΈλ€ LPDWORD lpNumberOfBytes, // μ μ‘λ byteμλ₯Ό λ΄μ λ³μν¬μΈν° PULONG_PTR lpCompletionKey, // μλ£ ν€ κ΅¬μ‘°μ²΄. βλ βμμ λκ²¨μ€ κ²μ. LPOVERLAPPED *lpOverlapped, // OVERLAPPED ꡬ쑰체. WSARecv, WSASendμμ λκ²¨μ£Όκ² λλ€. DWORD dwMilliseconds // νμμμ. );
5.4 μΈμμ μΈ μλ£ ν¨ν· 보λ΄κΈ° #
(보좩μλ§)
BOOL PostQueuedCompletionStatus( HANDLE CompletionPort, // CPνΈλ€ DWORD dwNumberOfBytesTransferred, // μ μ‘λ byteμλ₯Ό λ΄μ λ³μν¬μΈν° ULONG_PTR dwCompletionKey, // λκ²¨μ€ μλ£ ν€ κ΅¬μ‘°μ²΄. LPOVERLAPPED lpOverlapped // OVERLAPPED ꡬ쑰체. );
6.1 Completion Ports #
(λ²μμ μμμ΄ λ§μ΄ λ€μ΄κ°μμ λ°νλλ€. μ λ§€ν μ©μ΄λ κ·Έλ₯ μ μμ΅λλ€. - redpixel)
I/O completion ports are used with asynchronous I/O.
I/O CPλ λΉλκΈ° μ
μΆλ ₯κ³Ό ν¨κ» μ¬μ©λλ€.
The CreateIoCompletionPort function associates an I/O completion port with one or more file handles.
CreateIoCompletionPort ν¨μλ νλ μ΄μμ νμΌ νΈλ€κ³Ό CPλ₯Ό μ°κ²°μν€λ μν μ νλ€.
When an asynchronous I/O operation started on a file handle associated with a completion port is completed, an I/O completion packet is queued to the port.
CPμ μ°κ²°λ νμΌνΈλ€μμ λΉλκΈ° μ
μΆλ ₯μ΄ μ€νλ λ CP ν¨ν·μ CP λ΄λΆμ νμ μ μ¬λλ€.
This can be used to combine the synchronization point for multiple file handles into a single object.
μ΄κ²μ λ¨μΌ κ°μ²΄λ‘μ¨μ λ€μ€νμΌ νΈλ€μ μν λκΈ°μ μ²λ¦¬λ₯Ό κ²°ν©νκΈ°μν΄ μ¬μ©λ μ μλ€.(?)
A thread uses the GetQueuedCompletionStatus function to wait for a completion packet to be queued to the completion port, rather than waiting directly for the asynchronous I/O to complete.
μ°λ λλ μ§μ μ μΌλ‘ λΉλκΈ° μ
μΆλ ₯μ΄ μ’
λ£λκΈ°λ₯Ό κΈ°λ€λ¦¬κΈ° 보λ€λ, CPλ΄λΆμ νμ μ μ¬λ μλ£ ν¨ν·(completion packet)μ λ°κΈ°μν΄ GetQueuedCompletionStatusν¨μλ₯Ό μ¬μ©νμ¬ κ°μ μν μ μνν μ μλ€.
Threads that block their execution on a completion port are released in last-in-first-out (LIFO) order.
CPμμμ κ·Έλ€μ μ€νμ λΈλ‘λΉν μ°λ λλ LIFO μμλ‘μ¨ μ€νμ΄ μ¬κ°λλ€.
This means that when a completion packet is queued to the completion port, the system releases the last thread to block its execution on the port.
μ΄κ²μ CPμ μλ£ ν¨ν·μ΄ μ μ¬λμμ λ, μμ€ν
μ κ°μ₯ μ΅κ·Όμ ν¬νΈμμμ μ€νμ λΈλ‘λΉν μ°λ λλ₯Ό λμμν¨λ€λ κ²μ μλ―Ένλ€.
When a thread calls GetQueuedCompletionStatus, it is associated with the specified completion port until it exits, specifies a different completion port, or frees the completion port.
μ°λ λκ° GetQueuedCompletionStatusμ νΈμΆνλ©΄, κ·Έ ν¨μμ μ€νμ΄ λλ λκΉμ§ μ§μ λ CPμ μ°κ²°λλ©°, λ€λ₯Έ CPλ₯Ό μ§μ νκ³ CPλ₯Ό ν΄μ νλ€.
A thread can be associated with at most one completion port.
μ°λ λλ μ΅μν νκ°μ CPμ κ²°ν©λ μ μλ€.
The most important property of a completion port is the concurrency value.
CPμ κ°μ₯ μ€μν μμ±μ concurrency valueμ΄λ€.
The concurrency value of a completion port is specified when the completion port is created.
CPμ concurrency valueλ CPκ° μμ±λ λ μ§μ λλ€.
This value limits the number of runnable threads associated with the completion port.
μ΄ κ°μ CPμ κ²°ν©νμ¬ μ€νκ°λ₯ν μ°λ λμ μλ₯Ό μ ννλ€.
When the total number of runnable threads associated with the completion port reaches the concurrency value, the system blocks the execution of any subsequent threads that specify the completion port until the number of runnable threads associated with the completion port drops below the concurrency value.
CPμ κ²°ν©λ μ€νκ°λ₯ν μ°λ λμ μκ° concurrency valueμ λ€λ€λ₯΄λ©΄, μμ€ν
μ μ€νκ°λ₯ν μ°λ λμ μκ° concurrency valueμλλ‘ λ¨μ΄μ§λκΉμ§ μ΄λ€ λ€μμμλ₯Ό κΈ°λ€λ¦¬λ μ°λ λλΌλ μ€νμ λΈλμν€κ² λλ€.
The most efficient scenario occurs when there are completion packets waiting in the queue, but no waits can be satisfied because the port has reached its concurrency limit.
κ°μ₯ ν¨μ¨μ μΈ μλ리μ€λ μλ£ ν¨ν·μ΄ νμμ κΈ°λ€λ¦¬λ λμ CPκ° concurrency valueμ νκ³μ λλ¬νκ² λ§λλ λκΈ° μ°λ λκ° μλλ‘ νλ κ²μ΄λ€.
In this case, when a running thread calls GetQueuedCompletionStatus, it will immediately pick up the queued completion packet.
μ΄ κ²½μ°μλ μ€νμ€μΈ μ°λ λκ° GetQueuedCompletionStatusλ₯Ό νΈμΆνμ λ κ·Έκ²μ μ¦μ νμ μ μ¬λ μλ£ν¨ν·μ κ°μ Έμ€κ² λλ€.
No context switches will occur, because the running thread is continually picking up completion packets and the other threads are unable to run.
μ΄λλ context switchingμ΄ λ°μνμ§ μλλ€. μλλ©΄ μ€ννλ μ°λ λκ° μ°μμ μΌλ‘ μλ£ ν¨ν·μ νλνκΈ° λλ¬Έμ΄λ€. (μμ£Ό : μ΄λ λ€λ₯Έ μ°λ λλ κ°κ°μ μλ£ν¨ν·μ μ²λ¦¬νκ±°λ μ€νλμ§ λͺ»νκ³ λΈλλλ€.)
The best value to pick for the concurrency value is the number of CPUs on the machine.
concurrency valueλ₯Ό μ§μ νκΈ° μν κ°μ₯ μ΅μ μ μμΉλ μμ€ν
μμ CPUμ μμ΄λ€.
If your transaction required a lengthy computation, a larger concurrency value will allow more threads to run.
λ§μΌ μ¬λ¬λΆμ transactionμ²λ¦¬κ° μ΄λμ λ μκ°μ΄ 걸리λ κ³μ°μ΄λ©΄, μ’λ ν° concurrency valueλ₯Ό μ§μ ν¨μΌλ‘μ¨ λ λ§μ μ°λ λκ° μ€νλλ κ²μ νλ½ν μ μλ€.
Each transaction will take longer to complete, but more transactions will be processed at the same time.
κ° transactionμ μ’
λ£λλλ° λμ± μ€λ κ±Έλ¦¬κ² μ§λ§, λ³΄λ€ λ§μ transactionμ΄ λμμ μ²λ¦¬λ κ²μ΄λ€.
It is easy to experiment with the concurrency value to achieve the best effect for your application.
μ¬λ¬λΆμ μ΄ν리μΌμ΄μ
μ μν μ΅λνμ ν¨μ¨μ λ§μΆκΈ° μν΄ concurrency valueμ μ€ννλ κ²μ μ¬μ΄ μΌμΌκ²μ΄λ€.
The PostQueuedCompletionStatus function allows an application to queue its own special-purpose I/O completion packets to the completion port without starting an asynchronous I/O operation.
PostQueuedCompletionStatus ν¨μλ λΉλκΈ° μ
μΆλ ₯ μ²λ¦¬λ₯Ό μμνμ§ μκ³ CPμκ² νΉλ³ν λͺ©μ μ CP μλ£ ν¨ν·μ νμ μ μ¬ν λ μ¬μ©ν μ μλ€.
This is useful for notifying worker threads of external events.
μ΄κ²μ κ° μ°λ λμκ² μΈλΆ μ΄λ²€νΈλ₯Ό μΈμμν¬λ μ μ©νλ€.
The completion port is freed when there are no more references to it.
CPλ κ·Έκ²μ λμ΄μ μ°Έμ‘°νμ§ μμλ λ©λͺ¨λ¦¬μμ ν΄μ λλ€.
The completion port handle and every file handle associated with the completion port reference the completion port.
CPνΈλ€κ³Ό CPμ κ²°ν©λ κ° νμΌ νΈλ€μ CPλ₯Ό μ°Έμ‘°νλ€.
All the handles must be closed to free the completion port. To close the port handle, call the CloseHandle function.
CPλ₯Ό ν΄μ νκΈ° μν΄ λͺ¨λ νΈλ€λ€μ λ°λμ ν΄μ λμ΄μΌ νλ€. CPνΈλ€μ λ«κΈ° μν΄μ CloseHandleν¨μλ₯Ό μ¬μ©νλΌ.








