Implement mutual exclusion secrets in AJAX programs

Source: Internet
Author: User
As the AJAX model becomes more and more widely used, browser pages can keep the front-end user interface dynamic while requesting data from the backend server (thus called asynchronous in AJAX ). However, when the two sports visit the shared distinct CR at the same time

As the AJAX model becomes more and more widely used, browser pages can keep the front-end user interface dynamic while requesting data from the backend server (thus called asynchronous in AJAX ). However, when both movements visit the shared JavaScript and DOM data structures at the same time, the title is raised. JavaScript does not provide a classic solution for the concurrent program title. This article describes the author's new insights on the mutex mechanism. the verified mutex mechanism can play a good role in JavaScript.

  Why do we need mutual exclusion?

When multiple program logic threads visit the same data at the same time, the title is generated. The program generally assumes that the data it interacts with does not change during the interaction process. The code for visiting these shared data structures is called the critical section. a mechanism that only allows one program to visit at a time is called Mutex. In AJAX exploitation programs, this situation occurs when the code for asynchronous processing of the response from XMLHttpRequest simultaneously controls the data being applied on the user interface. This shared data may be JavaScript used to implement the MVC data model and/or DOM of the web page itself. If either of the two changes to the shared data, the logic of the two will be interrupted.

You may say, "Wait, why haven't I come across this title ?". Unfortunately, these titles are synchronously attached (also known as race conditions), so they are not always generated or may never be generated. Their probability is based on many factors. Based on tough considerations, Rich internet exploitation programs should prevent such situations by ensuring that these titles are not produced.

Therefore, a mutex mechanism is required to ensure that only one critical section can be opened at the same time and the other can be opened after it is completed. In most mainstream computer languages and performance frameworks, the mutual exclusion mechanism is provided (usually several), but the JavaScript used in the browser does not provide this mutual exclusion mechanism. Although there are some classic mutually exclusive implementation algorithms that do not require special language or environment support, some elements lacking in JavaScript and browsers (such as Internet Explorer) are still needed. Next, let's look at the classic algorithms that can play a good role in these browsers and languages.

  Bakery algorithm

In the computer science literature, the so-called Lamport bakery algorithm can be effectively used for multiple competing control threads, the communication between threads in this algorithm can only be performed in the shared memory (that is, special mechanisms such as semaphores and atomic set-and-test are not required ). The basic idea of this algorithm is derived from the bakery, because the bakery needs to get the number first and then wait for the call. Listing 1 shows the framework of this algorithm (derived from Wikipedia). This algorithm allows each thread to access the critical section without conflict.

Listing 1. algorithm pseudocode of Lamport bakery

// Declaration & initial values of global variables
Enter, Number: array [1 .. N] of integer = {0 };

// Logic used by each thread...
// Where '(a, B) <(c, d )'
// Means '(a <c) or (a = c) and (B <d ))'
Thread (I ){
While (true ){
Enter [I] = 1;
Number [I] = 1 max (Number [1],..., Number [N]);
Enter [I] = 0;
For (j = 1; j <= N; j ){
While (Enter [j]! = 0 ){
// Wait until thread j receives its number
}
While (Number [j]! = 0) & (Number [j], j) <(Number [I], I ))){
// Wait until threads with smaller numbers
// Or with the same number, but with higher
// Priority, finish their work
}
}
// Critical section...
Number [I] = 0;
// Non-critical section...
}
}

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.