Windows Server programming-Chapter 2 Communication Between device IO and threads-9-I/O completion of port management on the thread pool

Source: Internet
Author: User

LI/OManage the thread pool through the port

Now we should say why I/O finished ports are so useful. First, the number of threads that can run concurrently is specified when I/O completion port is created. As mentioned above, this value is usually the number of CPUs on the host, such as dual-CPU machines. When a complete I/O Request queue is generated, the I/O completion port will wake up the waiting thread. However, a completed port can only wake up a specified number of threads. Therefore, if four I/O requests are completed and four threads that call GetQueuedCompletionStatus are waiting, the I/O completion port will only wake up two threads; the other two threads continue to sleep. After the Awakened thread processes the I/O items, it calls GetQueuedCompletionStatus again. The system sees more items in the queue, so it wakes up the same thread again for processing.

 

If you think carefully, you will notice that some things do not make much sense: If the completion port only allows a specified number of threads to be concurrently awakened, why do extra threads need to wait in the thread pool? For example, if an I/O completion port is created on a dual-CPU machine, it is told that only two threads are allowed to simultaneously process finished I/O items. Then four threads are created in the thread pool (the number of CPUs multiplied by 2 ). The result is that it seems that two redundant threads are created and will never be awakened.

In fact, I/O finished ports are not that stupid. When a thread is awakened by the completion port, the thread ID is placed in the fourth data structure associated with the completion port: the list of released threads (see Table 2-1 ). In this way, the port will remember which threads are awakened and monitor the execution of these threads. If the released thread calls any function and sets itself to the waiting state, the completed port will detect and update the internal data structure: move the thread ID from the released thread list to the paused thread list (the fifth is the last data structure related to the I/O completion port ).

The goal of the completion port is to keep the thread in the released thread queue, which may be as many as the number of concurrent threads specified when the port is created. If a released thread enters the waiting state for any reason, the released thread queue will contract and the completion port will release another waiting thread. If the paused thread wakes up, it will leave the paused queue and re-enter the list of released threads. This indicates that the list of released threads can have more than the maximum number of concurrent threads allowed.

 

Note:

Once the thread calls GetQueuedCompletionStatus, It is scheduled to the specified completion port. The system assumes that all scheduled threads will work based on the specified port. The completion port only wakes up the thread from the thread pool when the number of threads being run is less than the maximum concurrency value of the completion port.

One of the following three methods can be used to interrupt the thread/complete the port arrangement.

1) Exit the thread

2) Let the thread call GetQueuedCompletionStatus and pass in another different I/O completion port

3) destroy the currently scheduled I/O completion port of the thread

 

Here is a summary. Suppose on a dual-CPU machine, create a completion port that allows no more than two threads to be awakened concurrently, and then create four threads to wait for completed I/O requests. If three completed I/O requests are generated on the port, only two threads are woken up to process these requests, reducing the number of running threads and saving switching time. If a running thread calls Sleep, WaitForSingleObject, WaitForMultipleObjects, SingalObjectAndWait, synchronous I/O calls, or any other function that will cause the thread to exit the running state, it will be detected by the I/O completed port and immediately wake up the third thread. The goal of port completion is to keep the CPU always working.

 

Finally, the third thread will become ready again. In this case, the number of threads that can be run is higher than the number of CPUs of the system. However, the port will realize this and no longer allow more threads to be awakened until the number of threads that can run is less than the number of CPUs. The I/O completed port architecture assumes that the number of threads that can run will only exceed the specified maximum number in a short time, it will soon fall back when the thread executes the next loop or calls GetQueuedCompletionStatus again. This explains that there will be more threads in the thread pool than the number of threads set to complete the port.

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.