Windows Server programming-Chapter 2 Communication Between device IO and threads-6-port created

Source: Internet
Author: User

LCreated Port

The principle of port completion is that there must be an upper limit for concurrent running threads, that is, 500 parallel clients do not allow 500 threads to exist simultaneously. So is it just the exact number of concurrent running threads? Yes, if you think about it, you will realize that if there are more than two running threads on a dual-CPU machine, and each CPU corresponds to one thread, it doesn't make much sense. Once the number of threads exceeds the number of available CPUs, the system has to spend time switching the threads, which will waste the CPU cycle, which is the inefficiency of the parallel model.

 

Another inefficiency of the parallel model is the creation of new threads for each client request. Compared with creating a new process in its own virtual address space, it is not free to create a thread. If a thread pool is set up during service application initialization, the performance will be improved. These threads can be used cyclically in the program. The I/O completion port is designed to work with a thread pool.

 

The I/O completion port may be the most complex Kernel Object. To create an I/O completion port, call the createiocompletionport function:

 

Handle createiocompletionport (

Handle hfile,

Handle hexistingcompport,

Ulong_ptr compkey,

DWORD dwnumberofconcurrentthreads );

 

This function executes two different tasks: Create an I/O completion port and associate the device with an I/O completion port. This function is quite complex. In my opinion, Microsoft should split it into two functions. When I use I/O to complete the port, I create two small functions to separate the two capabilities of createiocompletionport. The first function I created is createnewcompletionport. The implementation is as follows:

 

Handle createnewcompletionport (DWORD dwnumberofconcurrentthreads ){

 

Return (createiocompletionport (invalid_handle_value, null, 0,

Dwnumberofconcurrentthreads ));

}

 

This function has a parameter: dwnumberofconcurrentthreads. Internally, the createiocompletionport function is called. The hardcoded value is used as the first three parameters, and dwnumberofcocurrentthreads is used as the last one. We can see that the first three parameters of the createiocompletionport function are only useful when associating the device with the completed port (which will be discussed soon ). To create a port, I will use invalid_handle_value, null, and 0 to correspond to the first three parameters of createiocompletionport.

 

The dwnumberofconcurrentthreads parameter indicates the maximum number of threads that can run simultaneously on the port. If this parameter is set to 0, the number of completed ports is the same as the number of CPUs on the machine by default. This is useful to avoid extra thread switching. If the client request requires a long operation, increasing this value will reduce blocking,We strongly recommend that you do not add this value.. You can try different values to experience the performance of the application.

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.