While (1) troubles

Source: Internet
Author: User
Tags high cpu usage

The while (1) loop has never been seen before, that is, an endless loop, but it did not expect it to lead to a high CPU usage. in my previous article, I used DirectShow to write a video player with a CPU usage of up to 56%. I was so puzzled that I had no choice but to take the initial addition, deletion, and code debugging methods to find this bug. when I remove a while (1) loop in my program, the CPU is reduced to 6% at a moment. my God. Whie (1) Is there such a big skill? My while (1) loop is like this:

While (1)

{

Bool bconnected = connectnamedpipe (hpipe, null )? True: (getlasterror () = error_pipe_connected );

If (bconnected)

{

Respondpipemsg (hpipe );

}

}

I am a bit unconvinced. I guess it's not because of the Code in me. So I commented out all the code in while (1) and found that the CPU usage has not changed, still around 56%. it's really a bad idea. It's not just an endless loop. I didn't do anything in it. Don't do that. I did not give up, continue the test, I wrote a small program test:

# Include "stdio. H"

Int main ()

{

While (1)

{

 

}

Return 0;

}

A simple program can be used together with the conciseness of Hello world. However, the CPU usage is as high as 50%, and the memory usage is about 1 MB. my God. I don't define a variable. I just use a while, a return, load any library files, and link any header files without using 1 MB of memory. and the CPU usage is also 50%. What have I done? No computing ?... Confused. I didn't even notice this before. I only care about writing programs and implementing functions. Even if there is no obvious bug, I will be OK. No. You have to pay attention to the CPU usage and memory usage. the while (1) loop in my program is still required. I must check whether a client is connected to me through the pipeline at any time. however, the CPU usage is too unacceptable. for this reason, I also specifically investigated the usage of several audio and video playback software:

CPU memory

The value of the listener is 0, 4.5 m-6.5 m.

Kmplayer 5%-11% about 23 m

Qq player 4%-13% 10 m

The usage of the CPU and memory above may vary depending on different videos, but this is probably the case, in many cases, it is better than this (km and qq cpu usage are mostly 3%). I really don't know how they did it, such a low CPU usage and memory usage, in particular, the CPU usage is basically 0. my God. I have to solve my program problem quickly. Otherwise, my program cannot be used or is difficult to use. Because my program is used on a tablet, rather than ordinary computers, the program is optimized better. if any of your friends has encountered similar situations or experience in this area, give me some advice. in addition, the memory usage of my program is about 24 MB for playing audio and about 45 MB for playing video. It's really depressing.

 

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.