开发者

Multi-thread serialization in C#

开发者 https://www.devze.com 2023-02-05 08:41 出处:网络
Problem: I have large number of big messages to serialize and send over network. I would like to maximize performance, so I\'m thinking about creating multiple threads for message serialization and on

Problem: I have large number of big messages to serialize and send over network. I would like to maximize performance, so I'm thinking about creating multiple threads for message serialization and one thread for sending the data. Idea is to dynamically determine number of threads for serialization based on network performance. If data is sent fast and serialization is bottleneck, add more threads to b开发者_C百科oost serialization. If network is slow, use less threads and stop completely if send buffer is full.

Any ideas how to do that?

What would be the best algorithm to decide if more or less threads are needed?

How to correctly concatenate serialization results from multiple threads?

Please answer to any of this questions? Thanks


It can be treated as a Producer/Consumer problem, In Fx4 you can use a BlockingCollection.

But frankly I would expect the (network) I/O to be the bottleneck, not the serialization. You will have to measure.


You can chunk the data into packets and place the packets into a queue. The process that looks at network performance would look at the queue and decide how many packets to send. The downside to this implementation is that you will need to assemble the packets on the receiving end where they may not be received in the proper order.

0

精彩评论

暂无评论...
验证码 换一张
取 消