Multithreading – use non thread safe components with multithreaded components (Design)

Design issues:

Using non thread safe components in multithreaded components (collection, API,...)

Example:

Component1: a multithreaded socket server that sends messages... To message handlers

Component2: a non thread safe message handler that processes messages from the server

My solution

Add a thread safe component (buffer) between the server and the message handler. The buffer will receive messages from the server in a multi-threaded manner and send them to the message handler in a single threaded manner in the same order

My question:

Is there a better solution? Could the appropriate design pattern be agent or pipeline?

Solution

A good choice is to use producer / consumer mode

In this case, the multithreaded socket can act as multiple generators in the protected buffer, and the non thread safe message handler can use the message in its own thread completely synchronously This provides a very clean way to handle this type of scenario

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>