Linux c + + loop queue

 

In projects, there are often network message processing. Nowadays, there are many security cameras, usually an APP will be equipped to control IPC, such as setting up mobile detection, face recognition, baby cry recognition, etc. Clicking a button in the APP may trigger the APP to send several messages to the camera through the network. At this time, IPC needs to process these several or more messages. If the message is not processed concurrently by IPC, it needs to be received and processed continuously. At this time, by using the queue mechanism, the messages received first can be processed first. Messages received later wait, a typical first in, first out principle. If IPC processes messages concurrently, the queue mechanism can also be used, except that a lock mechanism is added to synchronize resources each time the queue is accessed. The message received by IPC may include APP user ID, requested command, message length, etc.

How to deal with the news? The general idea is as follows:
We can put the information of each message into the structure. In this way, each structure is a member of the queue. The thread receiving the message wraps the message into a structure and then adds it at the end of the queue (array). The thread processing the message takes out the queue head for parsing and processing. After each message is parsed and processed, the message is removed from the queue head. Therefore, we need to define an array of structs, which can also contain structs, etc., which can be expanded as long as the project needs.  
The program template is relatively simple, and the key is to know how to apply it to the project.  
What should be noticed is how to judge whether the circular queue is empty or full. Suppose the loop queue length is 5. When the head pointer and the tail pointer point to the same place, we set it to null. When there are elements in the queue, the tail pointer points to the next element, and when there are elements out of the queue, the head pointer points to the next element. When the element pointed to is 5, the next element is 0. In this way, the conclusion is that when the queue is full, the head pointer and the tail pointer are equal, which is the same as when the queue is empty. I don't believe it. Draw pictures.  
How to deal with that? In order to distinguish the empty queue and the full queue, an additional element is added to the array. This element is uncertain and can be moved. It will ensure that when the queue is full, a position is left empty. Speaking abstractly, look at the following code:

#include <stdio.h>
#include <string.h>


#define QUEUE_LEN 16
#define ARRAR_SIZE (QUEUE_LEN+1)

typedef struct student
{
    int math;
    int English;
    char name[32];
}student;


#define QUEUE_TYPE student





static student studentTable[ARRAR_SIZE];   //Define structure array
static unsigned int front;   //Point to queue head
static unsigned int tail;    //Point to the end of the queue


bool IsQueueEmpty(void)
{
    return (front == tail);
}

bool IsQueueFull()
{
    return ((tail + 1) % ARRAR_SIZE == front);
}

bool queueInsert(QUEUE_TYPE value)  //insert
{
    if(IsQueueFull())
            return false;
    studentTable[tail]=value;
    tail=(tail+1) % ARRAR_SIZE;  //tail

    return true;
}

bool queueDelete()      //delete
{
    if(IsQueueEmpty())
        return false;

    front=(front+1)%ARRAR_SIZE;   //head
     return true;
}


int main(int argc, char *argv[])
{
    student stu;
    stu.math=99;
    stu.English=98;
    char name[32]="salman1";

    memcpy(stu.name,name,sizeof(name));
    queueInsert(stu);
    stu.math = 61;
    stu.English = 60;
    memset(name,0,sizeof(name));
    sprintf(name,"xiaohong",sizeof(name));
    memcpy(stu.name,name,sizeof(name));
    queueInsert(stu);
    printf("front = %d,tail = %d,name = %s\n",front,tail,studentTable[front].name);
    queueDelete();
    printf("front = %d,tail = %d,name = %s\n",front,tail,studentTable[front].name);
    return 0;
    


}




 

Published 100 original articles, won praise and 10000 visitors+
Private letter follow

Keywords: network Mobile

Added by hbradshaw on Fri, 17 Jan 2020 15:17:26 +0200