Originally, windows also had signal.

Source: Internet
Author: User
Tags sleep function

In Windows, the signal mechanism is simply implemented by the working thread. The thread runs on the relative priority thread_priority_highest. When the signal is generated, Windows generates the thread to execute the signal processing logic, because the priority of this thread is usually higher than that of any thread explicitly created by the user, the Windows Thread Scheduling logic will block the execution of other threads until the worker thread exits after signal processing.

The following is a testCode

# Include "stdafx. H"
# Include <signal. h>
# Include <windows. h>

Using namespace STD;

Int J = 1;
Void onctrlc (INT ){

Cout <"Ctrl + C" <Endl;
Cout <: getcurrentthreadid () <Endl;
Cout <: getthreadpriority (: getcurrentthread () <Endl;

Signal (SIGINT, onctrlc );

J = 0;
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Signal (SIGINT, onctrlc );
Int I = 0;
 
//: Setthreadpriority (getcurrentthread (), thread_priority_time_critical );

While (true ){
Sleep (2000 );
Cout <++ I <Endl;
Cout <: getcurrentthreadid () <Endl;
Cout <: getthreadpriority (: getcurrentthread () <Endl;
// If (j = 0)
// Break;
}
Return 0;
}

This code can be tested in two ways

First: As shown aboveProgramPressing CTRL + C during running will cause onctrlc function execution. This execution can occur at any time, even in the master thread cout <100 <Endl;, only 10 is output, then execute onctrlc, and then output the remaining 0. the program output indicates that the threadid output by onctrlc is different from the threadid output by main, and the thredid output by onctrlc is constantly changing. This indicates that the thread of the function is re-created every time, onctrlc calls getthreadpriority to output 2, which is exactly thread_priority_highest, while main outputs 0, which is thread_priority_normal.

Second, comment out sleep (2000), open the setthreadpriority call in main, and release
If (j = 0)
Break;
Then run the program. This time, the priority of the main thread is adjusted to 15, which is higher than 2 of the onctrlc. Therefore, pressing CTRL + C during the program running will find that the program does not reflect anything, because the high priority of the main thread blocks onctrlc execution, this is why
If (j = 0)
Break;
If you do not release the code, the high-speed loop code will be rolled out before you notice that onctrlc is called. Since the priority is higher than onctrlc, how can onctrlc be called? This is because Windows dynamically improves the thread priority mechanism. Simply put, when Windows notices that a thread has been eager to be scheduled for 3-4 seconds, it will temporarily raise the priority to 15, in this way, the priority is equal to that of main, and everyone is not equal to each other. In this case, the program will exit after pressing CTRL + C for about 3 seconds.

Tip:

Only comment out sleep (2000), because the sleep function will allocate its CPU time to other threads.

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.