In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to achieve double cache queues in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas slowly and deeply. Let's study and learn how C++ can achieve double cache queues.
The details are as follows:
The "double cache queue" is a structure I designed for a particular scenario during a development task. The usage scenario is that the sender continuously sends packets to the receiver-regardless of whether the receiver completes the business logic or not. Since data loss may occur when the receiver stops responding under any circumstances, it is impossible to simply design a thread-safe queue to write or read data (when reading data, the queue lock is regarded as a stop response to writing).
The receiver first writes data to the A queue, then switches to the B queue to continue writing when the data processing request arrives, and then gives the data in the A queue to the data processing module. After the processing is completed, the A queue data is emptied. When the next data processing request arrives, the write request is switched back to queue A, and the data in queue B is submitted to the data processing module to empty queue B and take turns.
Once you have an idea, the code is relatively simple.
# include templateclass DoubleArray {struct NODE {T t; NODE* next;}; int size_a; int size_b; NODE* header_a; NODE* header_a_cur; NODE* header_b; NODE* header_b_cur; int trigger Public: DoubleArray (): size_a (0), size_b (0), trigger (0), header_a (0), header_a_cur (0), header_b (0), header_b_cur (0) {} int push (T t); std::list& fetch (std::list& list);}; templateint DoubleArray::push (T t) {NODE * n = new NODE; n-> t; n-> next = 0 If (size_a = = 0 & & trigger = 0) {header_a = n; header_a_cur = n; size_a++;} else if (size_b = = 0 & & trigger = = 1) {header_b = n; header_b_cur = n; size_b++;} else {switch (trigger) {case 0: header_a_cur- > next = n; header_a_cur = n Size_a++; break; case 1: header_b_cur- > next = n; header_b_cur = n; size_b++; break;}} templatestd::list& DoubleArray::fetch (std::list& list) {switch (trigger) {case 0: if (header_a! = 0) {/ / change b trigger = 1 / / fetch a NODE* temp = header_a; while (temp) {list.push_back (temp- > t); temp = temp- > next;} / / delete a temp = header_a; for (int I = 0; I
< size_a; ++i) { NODE* p = temp; temp = temp->Next; delete p;} size_a = 0; header_a = 0; header_a_cur = 0;} break; case 1: if (header_b! = 0) {/ / change a trigger = 0; / / fetch b NODE* temp = header_b / / delete b while (temp) {list.push_back (temp- > t); temp = temp- > next;} temp = header_b; for (int I = 0; I
< size_b; ++i) { NODE* p = temp; temp = temp->Next; delete p;} size_b = 0; header_b = 0; header_b_cur = 0;} break;} return list;}
Note: the development environment and IDE are respectively CentOS 7 and NetBeans 8.2
Thank you for your reading. the above is the content of "how to achieve double-cache queue in C++". After the study of this article, I believe you have a deeper understanding of how to achieve double-cache queue in C++. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.