The principle of multi-threading and CPU Resource allocation in window environment

Source: Internet
Author: User

First of all, in the WINDOWNT environment, Linux is a completely different effect, depending on the CPU resource allocation algorithm of the operating system.

Background introduction:

If there are three (or more) threads executing their own algorithm asynchronously (not knowing which SB writes the algorithm, rather consuming resources), then how is the CPU resources of the three threads allocated?

The first piece of code and

1   class Program2     {3         Static voidMain (string[] args)4         {5             inti =0;6              while(i< -)7             { 8                 NewThread (NewParameterizedthreadstart (o =9                 {TenConsole.WriteLine ("*************************"); Onei++; A                 })). Start (); -                 NewThread (NewParameterizedthreadstart (o = -                 { theConsole.WriteLine ("++++++++++++++++++++++++"); -i++; -                 })). Start (); -                 NewThread (NewParameterizedthreadstart (o = +                 { -                      while(I < -) +                     { A Console.WriteLine (i.ToString ()); ati++; -Thread.Sleep (0); -                     } -                 })). Start (); -             } - Console.read (); in  -         } to}


If the 24th line of comments, the results are very different, then Threadsleep (0) The role of the obvious, although from the code logically understand that it should be a thread sleep 0 milliseconds, want to do more than anything, for the Windows operating system is very different, this sentence meaning system understood as This time does not participate in CPU resource competition.

Because the CPU resource allocation of window is allocated in the way of "democracy", all the threads that need resources are queued in the system, the system will give them a rating, which one needs resources most, which one least needs resources, which is equivalent to adding a weight to each resource applicant (if the applicant is a beauty, Weight is naturally higher). When the previous user freed the CPU, the system will arrange the next to use the CPU (said so much, did not see what the difference with Linux, but the following is not the same), when the user consumes the CPU, must occupy the active release, otherwise the CPU will always be occupied.

Therefore, when doing multithreading should pay attention to the release of resources.

If there is a mistake, look correct!

The principle of multi-threading and CPU Resource allocation in window environment

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.