[C ++] reasonable design and use of Message Queues

Source: Internet
Author: User

Producer and consumer issues are always classic issues.

Simply making multiple threads compete, occupying resources and then processing them will make the complexity of the system quite complex, and it is difficult to control the concurrency of the entire system. to simplify and streamline the system, the message queue is introduced, and the system has a high throughput. because it is simple and effective.

Depending on the specific business, I personally (think) divided the message queue into a model, and N: M model.

IfHigh Consumption capability. In my opinion, we should use a queue. this queue is simple enough for various optimizations, such as avoiding dynamic memory allocation, locking optimization, and using lock-free. in this way, the wait time of the main loop can be minimized, so that it can concentrate on various logic processing.

Sometimes, some business logic does not care much.HourA relatively small time error is allowed, and the consumption capability is very strong. A queue of can also be used. However, if the number of producers is N, the number of consumers is still one. :-D. Of course, there are not many such scenarios.

Here is a lock free message queue.

IfPoor consumption abilityThere are also some businesses, such as those related to money, such as database archiving, and such a system can only use the N: M model to increase throughput. but here we replace lock-free with mutex. Although mutex may fall into the kernel, as long as the critical section is small enough, the operation is lightweight enough, and the efficiency is quite high. general systems do not have bottlenecks here.

Here we provide the Implementation of blocking queue.

Consumption speed \ Time Series Concern Don't care
Slow consumption Blocking Blocking
Fast consumption

1:1 lock free

N: m blcoking

Lock free

In fact, a lot of businesses, is enough fast.

PS: Actually, the lock free queue of N: m can also be implemented, but CAS is required. fecth and add make n concurrent operations impossible. in addition, the complexity of implementing CAS message queues is quite high, and the benefits are not very effective (in the case of a large number of CPU cores), so the lock free queue with multiple concurrency is abandoned.

In addition, you do not care about the time sequence. It does not mean that you do not care about it at all. Instead, you are allowed to have a certain error, for example, 10 ms.

I still want to postCode, Think about it. The code is open-source on GitHub and can be implemented by yourself.

Https://github.com/egmkang/green_turtle/blob/master/green_turtle/blocking_queue.h

Https://github.com/egmkang/green_turtle/blob/master/green_turtle/message_queue.h

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.