dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: CPU at 100% when using AfxBeginThread

Hybrid View

  1. #1
    Join Date
    Jan 2015
    Posts
    8

    CPU at 100% when using AfxBeginThread

    I have a program that uses multiple threads that are started with AfxBeginThread. On a low end systems (Duo Core2, 5 GB Ram) I have on thread, that even if it is set up as a somple a thread as can be:

    while(true)
    {
    Sleep(500);
    }

    The CPU Usage goes to 100% and stays there.
    If I fill out the code to process my data, it goes to 100%.

    If I run on a powerful system (Core i7, quad core, 28GB Ram) there is no problem.

    I can find nothing that says what the can cause this. The memory used across either platform stays even the entire time the thread is running. For example, if the memory used is 5.7GB on my big system or 2.8 on the small one, it that is the level it stays at, NO FLUCTUATIONS. No memory leaks. I also have run this in debug (Visual Studio 2013 w/DeLeaker) and no memory leaks.

    Finally, the only solution I have been able to come up with is to stop and start the thread if the CPU% gets over 80%. this works and keeps the thread from pegging at 100% and staying there. But that doesn't seem a good solution.

    Anybody have any idea what could be causing this?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,147

    Re: CPU at 100% when using AfxBeginThread

    How many threads are you starting? What is the thread priority for each thread?

  3. #3
    Join Date
    Jan 2015
    Posts
    8

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by Arjay View Post
    How many threads are you starting? What is the thread priority for each thread?
    Only 3, but it is only one thread that is causing the problem.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CPU at 100% when using AfxBeginThread

    Why do people still think that the CPU being at 100% is necessarily a bad thing?

    That is actually the "perfect" case scenario for a well written, well implemented and well balanced MT problem.

    The whole point is keeping the CPU busy "all the time". ANy time the CPU is not at 100%, your code is "running" but not actually doing anything useful.

    If you suspect something is wrong, you need to look at other things to indicate this. Which ones is hard to tell without knowing what your code does.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,147

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by OReubens View Post
    Why do people still think that the CPU being at 100% is necessarily a bad thing?
    100% will often result in a non-responsive UI - not too cool if a user is logged on and trying to use the system. For folks new to multi-threaded programming, pegged cpu's could be an indication of doing something wrong.

  6. #6
    Join Date
    Jan 2015
    Posts
    3

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by Arjay View Post
    100% will often result in a non-responsive UI - not too cool if a user is logged on and trying to use the system. For folks new to multi-threaded programming, pegged cpu's could be an indication of doing something wrong.
    Fully agree

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by Alex_41 View Post
    Fully agree
    BUt the Two are unrelated.
    it is perfectly possible to have the CPU's at 100% and have a perfectly fine responsive app. In fact that is the situation to strive for.


    A badly written MT app may cause the UI to be unresponsive and this can happen regardless of the CPU being at 100% or not. You can have a badly responsive app with the CPU being 0%.


    So I disagree. pegged CPU's are not a (good) indication of doing something wrong.
    A thread not producing results within expected time is an indication of doing something wrong (which is the same as it would be for a single threaded app).
    THe UI being unresponsive or hanging is an indication of doing something wrong

  8. #8
    Join Date
    Jan 2015
    Posts
    8

    Re: CPU at 100% when using AfxBeginThread

    All of what you all say is good advice and reasonable but non of it is valid in this case. Handles don't increase, all resources are cleaned up (and Deleaker doesn't find anything leaking.) It is not a "slow leak", this runs a few seconds and is at 100% CPU usage.

    Thanks all.

  9. #9
    Join Date
    Feb 2015
    Posts
    2

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by WildWill View Post
    All of what you all say is good advice and reasonable but non of it is valid in this case. Handles don't increase, all resources are cleaned up (and Deleaker doesn't find anything leaking.) It is not a "slow leak", this runs a few seconds and is at 100% CPU usage.

    Thanks all.


    May be you found a solution in the meanwhile.

    I had the same problem when porting a program from visual Studio 6.0 to 2005.
    The threads were started wiwth AfxBeginThread and the Sleep() function was used to
    make the timing. On using the CreateThread Function all was fine.

    As I think using Sleep() is allways a bad solution to do thrad timing, or evan at all
    I searched for an alternative.

    So trie out finding the WaitableTimer in the Microsoft dokumentation and try using this!

    I would advice you never using Sleep() but real Timer functions or event driven synchronization.

    Hope it helps

  10. #10
    Join Date
    Feb 2015
    Posts
    2

    Re: CPU at 100% when using AfxBeginThread

    May be I didnīt explain exactly enough what I ment.

    Please go into your debuger with yout thread where you have placed your Sleep function.
    And see what happens when the debugger tries to execute the Sleep function.

    In my case the Sleep function acted just like an empty line. Itīs strange, I know. Also for me.
    So I put the WaitableTimer as an replacement for the Sleep function an it worked.

    I also had other threads in my application that were startes through the CreateThread Methode.
    In this case I had no problems with CPU overload.

    Also there exists another type of Sleep the SleepEx function.
    May be this one acts different and work too?

    The WaitableTimer is a real timer Sleep is strange. Write it as a replacement for Sleep
    You will find stuff at msdn.
    Someone wrote an article about:
    https://randomascii.wordpress.com/20...-investigated/

    And may be this helps too:
    http://www.codeproject.com/Articles/...imers-Tutorial


    And there are different ways of better use for timers sa a replacement of Sleep.
    Watch out for them.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,910

    Re: CPU at 100% when using AfxBeginThread

    I don't see how having multiple threads with a body of
    Code:
    while(true)
    {
    Sleep(500);
    }
    would cause the cpu utilisation to go to 100% and permanently stay at 100%? How many threads and what else is running on the system? I've tried it on my systems with a program that creates 100 threads and I see no increase in cpu utilisation from before (without the threads) to during the program (with 100 threads).

    However, this code would cause 100% utilisation
    Code:
    while(true);
    {
    Sleep(500);
    }
    PS with 1000 threads Resource Monitor is reporting 0 cpu for the program!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  12. #12
    Join Date
    Jan 2015
    Posts
    8

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by 2kaud View Post
    I don't see how having multiple threads with a body of
    Code:
    while(true)
    {
    Sleep(500);
    }
    would cause the cpu utilisation to go to 100% and permanently stay at 100%? How many threads and what else is running on the system? I've tried it on my systems with a program that creates 100 threads and I see no increase in cpu utilisation from before (without the threads) to during the program (with 100 threads).

    However, this code would cause 100% utilisation
    Code:
    while(true);
    {
    Sleep(500);
    }
    PS with 1000 threads Resource Monitor is reporting 0 cpu for the program!
    I am not a noob. I have been doing MT for years. This is just one that doesn't make sense. I don't have a good answer but a rather a crappy work around (kill and restart the thread everytime it get over 80%). And Arjay is right. When this happens users can get anything else done, so it is not good that it is at 100%.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,147

    Re: CPU at 100% when using AfxBeginThread

    LOL. Took me a while to see the difference.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,910

    Re: CPU at 100% when using AfxBeginThread

    So what is this one thread doing? Can you post a complete test program that demonstrates the problem.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  15. #15
    Join Date
    Jan 2015
    Posts
    8

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by 2kaud View Post
    So what is this one thread doing? Can you post a complete test program that demonstrates the problem.
    No, I can't post a test program as the test programs do not show the problem. The other two threads I have are set up the same way and do not exhibit this issue. Here is the declaration and calls

    Code:
    .h File
    
    private:
    	static UINT			MonitorComPortThread(LPVOID pParam);
    	void				MonitorComPortThread();
    	static UINT			MonitorComPortForBytesThread(LPVOID pParam);
    	void				MonitorComPortForBytesThread();
    	static UINT			MonitorCPUUsageThread(LPVOID pParam);
    	void				MonitorCPUUsageThread();
    
    .cpp file
    		AfxBeginThread(ProcessSerialPortDataThread, this);
    		AfxBeginThread(MonitorComPortThread, this);
    
    		if (bRunCPUUsageThread)
    		{
    			AfxBeginThread(MonitorCPUUsageThread, this);
    		}
    
    UINT CSerialCom::MonitorComPortThread(LPVOID pParam)
    {
    	CSerialCom* pSerialCom = (CSerialCom*)pParam;
    	pSerialCom->MonitorComPortThread();
    	return 0;
    }
    
    // WLL ZK UPDATE
    UINT CSerialCom::ProcessSerialPortDataThread(LPVOID pParam)
    {
    	CSerialCom* pSerialCom = (CSerialCom*)pParam;
    	pSerialCom->ProcessSerialPortDataThread();
    	return 0;
    }
    
    UINT CSerialCom::MonitorCPUUsageThread(LPVOID pParam)
    {
    	CSerialCom* pSerialCom = (CSerialCom*)pParam;
    	pSerialCom->MonitorCPUUsageThread();
    	return 0;
    }
    
    and just the MonitorComPortThread with "no body"
    void  CSerialCom::MonitorComPortThread()
    {
    	try
    	{
                while(true)
                {
                     Sleep(500);
                }
            }
            catch(...)
            {
               AfxEndThread(1);
               return;
            }
    }
    Last edited by Arjay; January 13th, 2015 at 07:48 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)