Breaking ReadFile() blocking - Named Pipe (Windows API)

后端 未结 7 706
广开言路
广开言路 2020-12-30 23:43

To simplify, this is a situation where a NamedPipe SERVER is waiting for a NamedPipe CLIENT to write to the pipe (using WriteFile())

The Windows API that is blocking

7条回答
  •  悲&欢浪女
    2020-12-30 23:51

    Mike,

    You can't cancel synchronous ReadFile. But you can switch to asynchronous (overlapped) operations. By doing this, you can implement a pretty scalable architecture.

    Possible algorithm (just an idea):

    • For each new client call ReadFile
    • WaitForMultipleObjects where the handles are overlapped.hEvent + your custom events
    • Iterate over signalled events, and schedule them for execution by threads from a threads pool.

    This way you can have only few threads to receive connections and read data, while the actual data processing can be done by the threads pool.

提交回复
热议问题