Line Cheng warning Status and APC queue _ Driven development

Source: Internet
Author: User
Tags apc

Turn from: http://blog.csdn.net/qq_22423659/article/details/53425828

The real meaning of the warning state of line Cheng

1, through other methods to let the thread "pause" (SuspendThread methods, such as sleepex,wait function family, etc.), and can enter a call

State of Alterable (warning state)

2, this state is essentially a high-speed system scheduler: The current out-of-the-box function call stack and the corresponding register state can be directly used to call

Other functions, which are generally called asynchronous functions

3. The system usually uses this thread environment (understood as the function caller) to invoke some other callback functions (e.g. IO completion notification, thread asynchronous call queue, etc.)

4. The advantage of this is that the system does not have to create some new dedicated threads for some callback functions, which can achieve the goal of saving system resources.

can also give full play to system performance

5, usually asynchronous functions should be done quickly, do not do too time-consuming operation, or the real thread function needs to execute, will not immediately execute

Reference Video: Click on the Open link

Second, the thread APC queue

1. Each thread can explicitly create an "asynchronous call queue" by calling the QUEUEUSERAPC function

2, in fact, is a thread on the line function call stack to arrange a set of functions to perform

3. By default, this queue is not created when a thread is created, and this queue is created for this thread when the function is called

4, create the APC queue function, generally use the wait function family or SleepEx function, such as the function with balertable parameters into a false "pause" state

5, into the alertable state of the thread, the system scheduler will be in the thread function itself in a "paused" (wait state), one call to the function in the line APC queue

6. Note the functions in the APC queue do not perform events too long to affect the execution of the thread function itself

7, it should be noted that some functions may also cause the thread to enter the waiting state, but can not enter a warning state, that is, can not invoke asynchronous functions, such as:

GetMessage functions, etc. (these functions also have no balterable parameters)

8, finally also need to note that, do not call in the APC function to let the thread into the alterable state of the API, which will cause a recursive, and cause the thread stack Overflow


[cpp]  View Plain  copy/*  demonstrates how threadalerable can allow a thread to enter a warning state and execute functions   */   in an APC queue include<windows.h>   #include <tchar.h>   #include <strsafe.h>      #define &NBSP;GRS_USEPRINTF ()  TCHAR pBuf[1024]={}   #define &NBSP;GRS_PRINTF (...) \       stringcchprintf (pbuf, 1024, __va_args__); \       writeconsole (GetStdHandle (Std_output_handle),  pbuf, lstrlen (PBuf),  NULL ,  null);      void callback apcfunction (ulong_ptr dwparam)    {        int i = dwParam;       grs_ useprintf ();       grs_printf (_t ("%d apc function run!\n"), i);        sleep (;  )}      int _tmain ()    {       //for main thread add APC function        for  (int i  = 0; i < 100; i++)        {     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;QUEUEUSERAPC (Apcfunction,getcurrentthread (), i);// Create apcfunction queues for current threads        }       //through SleepEx entry " Warning status ", after comment, the APC function will not be invoked        //sleepex the main thread into the wait, perform the APC function         sleepex (10000,true);       _tsystem (_t ("PAUSE"));        return 0;  }  

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.