Message Queue, Message Queue java
When designing a message queue, we used double buffering to keep one read thread and one write thread !! The read thread operates on read_list and the write thread operates on write_list to keep the complete row of a queue!
For example, if you want to keep the write_list consistent, it is the cool of the insert operation. When the read_list is empty, it needs to be exchanged with the write_list. It also requires cool !! This ensures very few thread conflicts !!! At that time, the Message Queue Dual-buffering was designed using std: list!
The message queue of the second version has been greatly improved, using three buffer queues, one read queue, one write queue, and one Buffer Queue, the three queues are represented in arrays. As long as the consistency of the buffer queue is ensured, the cool is required when the write queue submits data to the buffer queue, read queues need to be shackled when they exchange data to the buffer queue! Compared with the above method, the number of shackles is significantly reduced, and the arrays are used, which is more efficient than std: list! This message queue is designed based on the original company's dual-Buffer Queue!
The test code is provided! And some interfaces!
+ View Code
Interface:
class DuplexList { public : DuplexList( int size = 1024); virtual ~DuplexList( void ); virtual int append(CommonItem item); virtual int peek(CommonItem &item); protected : virtual int swap(CommonItem &item); virtual int submit(); protected : CommonList *read_list_; CommonList *share_list_; CommonList *write_list_; Mutex _switch_mutex; int peek_index_; }; |
After one afternoon of testing, there is no problem. It takes about 20 ms to transmit 10 million messages! This performance is fine!
I hesitated for a long time on the language selection. Some java friends advised me to say that a message queue error would cause the entire game to crash! Advise me to use the java integration structure !! However, I finally insisted on C ++. The underlying code was written in C ++ and the logic was written in lua! I just have a lot of questions about AI!