Can I create multiple I/O Completion Ports in a single application? I mean, hold two or more CreateIoCompletionPort handles with their own CompletionKey's? My application has 2 IOCP Classes with their own client structures starting from the index 0. I use these indexes in the CompletionKey so I believe that in some point this causing conflict because my application leads to a deadlock without any logical reason. Triple checked for any deadlock situation and run in debugging mode not helped!
Asked
Active
Viewed 470 times
1 Answers
2
Yes. You can create as many IOCPs as you like*.
I expect you have a bug in your code or a standard 'deadlock caused by lock inversions'.
Can you break into the app in the debugger when it has deadlocked and see what the threads are doing?
(* subject to the usual resource limitations, memory, etc).

Len Holgate
- 21,282
- 4
- 45
- 92
-
Thanks. I read in your website about lock inversions and I'm pretty sure that's it. Now I have another problem... I can't figure out how to fix the code, because I need these locks. In my application I have 2 IOCP's with their own client struct with per client critical section. Some threads locks on IOCP 1 then on IOCP 2 and other threads locks on IOCP 2 then on IOCP 1 (sometimes causing the lock inversion). How can I deal with it? – Victor Volpe Jul 14 '14 at 00:58
-
Assuming that you have some kind of gateway/proxy situation here where you have two connections that you link together then the solution is to have a single piece of "linking data" which has its own lock. You then link the connections to the linking data by locking each connection to add the link; but never holding both connection's locks at the same time. Once linked you only use the link to access the connections and only ever hold the link's lock when working with the two connections. – Len Holgate Jul 14 '14 at 20:21
-
I have an Anti Hack System for Online Game. The client and game server connects to my application. One IOCP is public (clients) and the another is private (game servers). The problem is that I need to lock in both data structs sometimes and it is impossible to have just an single piece of linking data... – Victor Volpe Jul 14 '14 at 20:45
-
There's no need to have two IOCPs. You can have any number of listening sockets associated with a single IOCP and associate all of your connections to the same IOCP. Use a flag or something in the per-connection data to differentiate between the two 'endpoints' that are listening (client and game). Note this still requires that you link two connections. – Len Holgate Jul 15 '14 at 07:10
-
1Another approach to avoid deadlock in situations with two locks is to always lock the locks in memory address order. This will mean you will always lock the locks in the same order and you wont get the lock inversion. – Len Holgate Jul 15 '14 at 07:11