Data Structure queue _ queue instance: event processing, queue instance

Source: Internet
Author: User

Data Structure queue _ queue instance: event processing, queue instance

Using queues to process events in event-driven applications is a common method.

Event-driven applicationsMainly follows the sequence of real-time events..

For example, when developing a graphical user interface in java or windows, the behavior of an application mainly depends on keyboard operations, mouse clicks, and other user-triggered events. Other data-driven examples include control systems in aircraft or factory equipment.

In many event-driven applications, events may occur at any time. Therefore, it is important to store and manage these events in an orderly manner before they can be processed.Because the system processes events in the order they occur, the queue is a good way to handle this situation.

Example 1 lists two functions used for event processing:Receive_event and process_event. Both functions are used to process queues that contain Event Events. Event is defined in event. h and is not listed here.

An application calls receive_enent to queue events to be processed. When an application considers it a time to process an event, it calls the process_event function.

Within the process_event function, events are queued and transferred to the specific scheduling function specified by the application. The scheduling function is passed to process_event as the parameter dispatch. The purpose of using a scheduling function is to take appropriate actions to handle events.

There are two common Scheduling Methods:Process events synchronouslyThat is, the next operation cannot be performed before the event is handled;Process events AsynchronouslyThat is, in the process of event processing, an independent process can also be started to process other events. Generally, asynchronous processing is more efficient, but you need to be careful when dealing with the relationship between the master and slave processes to avoid conflicts.

The running complexity of receive_event is O (1), because it only calls queue operation queue_enqueue whose complexity is O (1. The running complexity of process_event depends on the scheduling function called by process_event. The remaining parts of process_event run at a fixed time.

Example 1: Implementation of event processing functions
/*events.c*/#include <stdlib.h>#include <string.h>#include "event.h"#include "events.h"#include "queue.h"int receive_event(Queue *enents,const Event *event){    Event  *new_event;        if((new_event = (Event*)malloc(sizeof(Event))) == NULL)        return -1;        memcpy(new_event,event,sizeof(Event));    if(queue_enqueue(events,new_event) != 0)        return -1;    return 0;}int process_event(Queue *events,int (*dispatch)(Event *event)){    Event  *event;        if(queue_size(events) == 0)        return -1;    else    {        if(queue_dequeue(events,(void**)&event) != 0)            return -1;        else        {            dispatch(event);            free(event);        }    }return 0;}

 

Related Article

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.