Research on timeout mechanism implementation

Source: Internet
Author: User

In many actual server programs, return if time out (return if time out) is required for service processing. That is to say, each service cannot be processed without restrictions, the server program cannot be blocked in the location where the business logic is processed and the subsequent tasks cannot be run because a service has not been run for a long time, the establishment of the timeout mechanism becomes necessary. Simply put, once the service processing time exceeds the value set by timeout, for example, 10 seconds, the service will be returned in advance if it is no longer executed, on the contrary, if the processing time of the service does not exceed the value set by timeout, the service returns normally after it is executed, set the timeout value so that the length of time the server program processes the service is not longer than a specific value.

It's easy to say and difficult to do. How can we use code to implement such a mechanism? I have studied for a while. After several spiral loops of affirmation-negation, I have finally fixed the problem, later, my friends said that Huawei's time lock also adopted this idea! As shown in the table, the following describes my thoughts in two aspects under prosperous network resources:

1. Train of Thought: set a global flag and initialize it to identify whether a specific service has been executed. The specific practice is to modify its value when the service starts to be executed, change back to the original value before the execution is completed and exit. in other places of the program, you can check whether the value of the global variable has changed to determine whether the business logic has been completed. In addition, the multi-thread concurrent execution mode must be adopted. It is impossible to start timing after the business logic is executed or before the execution is started. That's too Sb, So logically speaking, the execution of business logic and timing should be carried out at the same time, just like a running competition. Your running and coach timing are carried out at the same time, if you run again after the coach times out or when you run out and start timing again, it will become a joke for Chinese people to talk about money after meals!

2. To do a good job, you must first sharpen the tool. You must review a key tool: pthread_cancel. This is a good plug-in. You can rape another thread in one thread to end another thread.

 

The following is code implementation:

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <pthread. h>
# Include <time. h>
 
// Basically, the research is clear.
Void * print_message_function (void * PTR );
Void * print_message (void * PTR );
Void * print (void * PTR );
Int I = 0;

Int flag = 0; // use this value to indicate thread switching.
Pthread_t thread1;
Pthread_t thread3;

Main ()
{

Char * message1 = "thread 1 ";
Int iret1;
Iret1 = pthread_create (& thread1, null, print_message_function, (void *) message1 );
Pthread_join (thread1, null );
Printf ("exit the entire program! /N ");
Exit (0 );
}

Void * print_message_function (void * PTR)
{
Time_t cur1, cur2;
Cur1 = Time (null );
Char * message;
Message = (char *) PTR;
Printf ("% s/n", message );
Int iret3;
Flag = 11;
Iret3 = pthread_create (& thread3, null, print, "My name is 007 ");
Sleep (10); // simulate the specific business logic. Assume that the execution takes 10 seconds.
Flag = 0;
Cur2 = Time (null );
Printf ("function print_message_function execution time is % ld s/N", cur2-cur1 );
Pthread_join (thread3, null );
}
Void * print (void * PTR)
{
Printf ("flag = % d, exit! % S/n ", flag, (char *) PTR );
Sleep (2); // here 2 is the set value of timeout
If (FLAG)
Pthread_cancel (thread1 );
}

 

Output:

[Root @ localhost posixthread] # mytimer
Thread 1
Flag = 11. Exit! My name is 007
Exit the entire program!

 

It indicates that thread 1 exits early because the execution time is 10 seconds and exceeds the timeout value set for thread 3 minutes. In fact, the program exited after being executed for more than two seconds (two seconds are the time for processing the business logic, and most of the time for executing other programs)

Modify program parameters as follows:

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <pthread. h>
# Include <time. h>
 
// Basically, the research is clear.
Void * print_message_function (void * PTR );
Void * print_message (void * PTR );
Void * print (void * PTR );
Int I = 0;

Int flag = 0; // use this value to indicate thread switching.
Pthread_t thread1;
Pthread_t thread3;

Main ()
{

Char * message1 = "thread 1 ";
Int iret1;
Iret1 = pthread_create (& thread1, null, print_message_function, (void *) message1 );
Pthread_join (thread1, null );
Printf ("exit the entire program! /N ");
Exit (0 );
}

Void * print_message_function (void * PTR)
{
Time_t cur1, cur2;
Cur1 = Time (null );
Char * message;
Message = (char *) PTR;
Printf ("% s/n", message );
Int iret3;
Flag = 11;
Iret3 = pthread_create (& thread3, null, print, "My name is 007 ");
Sleep (2); // simulate the specific business logic. Assume that the execution takes 10 seconds.
Flag = 0;
Cur2 = Time (null );
Printf ("function print_message_function execution time is % ld s/N", cur2-cur1 );
Pthread_join (thread3, null );
}
Void * print (void * PTR)
{
Printf ("flag = % d, exit! % S/n ", flag, (char *) PTR );
Sleep (10); // here 2 is the set value of timeout.
If (FLAG)
Pthread_cancel (thread1 );
}

 

[Root @ localhost posixthread] # mytimer
Thread 1
Flag = 11. Exit! My name is 007
The execution time of the function print_message_function is 2 seconds.
Exit the entire program!

 

It can be seen that the set timeout is 10 seconds, which is longer than the time needed to process the business logic. The business logic is no longer needed. You only need to wait for the execution to complete (2 seconds) and then exit.

 

 

The above program simply simulates the timer idea, but there is no doubt that it implements the two opposite logic of the timer (What should we do when the timer times out, what should we do if there is no timeout ), we can further encapsulate it and provide external interfaces, such as setting the timeout value and the business logic entry, to become our own timer. It will be very convenient to call later.

 

 

 

 

 

 

 

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.