Server application-Dual-Buffer Queue

Source: Internet
Author: User

In server development, the common practice is to separate the logic processing thread from the I/O processing thread.

Logic processing thread: Performs logic processing on received packets.

I/0 processing thread: sending and receiving network data, establishing and maintaining connections.

Generally, the logic processing thread and the I/O processing thread exchange data through the data queue, that is, the producer-consumer model.

This data queue is shared by multiple threads and requires locks for each access. Therefore, it is particularly important to reduce the overhead of mutex/synchronization.

Solution: Dual-Buffer Queue

Background:

The so-called double buffer data is two queues, one responsible for writing data from, and the other responsible for reading data. After reading data, the logic thread is responsible for exchanging its own queue and the queue of the I/O thread.

In this case, two data writes from the queue and the two queues need to be locked for exchange. If a buffer zone is used, read and write operations are not separated. The dual-buffer zone saves at least the overhead of mutex/synchronization for read operations in a single buffer zone. In essence, it is the optimization idea that uses time for space.

Buffer Status Analysis:

The two buffers correspond to two mutex Locka and lockb respectively. The producer consumer must first obtain the corresponding lock to control the buffer.

1. Concurrent read/write

In most cases, the producer controls a queue for write operations, and the consumer controls another queue for read operations.

That is to say, the logic thread and the I/O thread perform exclusive operations. This greatly reduces the overhead of mutex/synchronization.

2 buffer Switching

When the consumer reads his queue (corresponding to Locka), the control on Locka is immediately released, waiting for the control of lockb. Once the producer releases lockb, the consumer immediately controls lockb and starts to read the queue data corresponding to lockb. At the same time, the producer controlled Locka and started the write operation. This completes the queue exchange.

Note that the producer and consumer must release the lock resource before trying to control another queue after completing the queue operation.

If the producer tries to control another lock without releasing its own lock after the operation is completed, and the consumer tries to control another lock without releasing the resource, a "deadlock" will occur ".

Queue switching policy analysis:

Read is guaranteed first, that is, after the consumer completes operations on the queue, the consumer exchanges the message immediately.

The first two queues are empty. The switching thread is blocked at the condition variable of the producer, and the working thread is blocked at the condition variable of the consumer. When the number of data in the producer queue is greater than 1, this condition notification is immediately sent, A queue switch occurs immediately. After the switch is complete, it is found that when the number of data in the consumer queue is greater than 1, the consumer condition variable notification is sent immediately. At this time, the switching thread is blocked at the condition variable of the consumer, the worker thread ends and waits to start processing data. After the processing is complete, a conditional notification is sent from the switching thread, and a conditional variable is sent to notify the switching thread to continue working.

Make sure that:

1. If the worker thread processes data and ensures no queue exchange, it can normally put data into the producer queue.

2. After the queue is switched, the switching thread needs to immediately wait for the condition notification that the consumer thread has completed execution. At this time, data can be placed into the producer queue.

3. Two actions need to be taken after the consumer queue is processed: 1. Wake up the switching thread and send the corresponding conditional variable notification. 2. Conditional notification waiting for the switching thread to complete one queue exchange.

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.