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

  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 online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    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
    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.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    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.

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

    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)

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: CPU at 100% when using AfxBeginThread

    LOL. Took me a while to see the difference.

  7. #7
    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.

  8. #8
    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%.

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

    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)

  10. #10
    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.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,545

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by WildWill View Post
    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
    It would help a lot if you added the code tags around the code snippets.
    Victor Nijegorodov

  12. #12
    Join Date
    Jan 2015
    Posts
    8

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by VictorN View Post
    It would help a lot if you added the code tags around the code snippets.
    Not sure what you mean by "code tags"?

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

    Re: CPU at 100% when using AfxBeginThread

    Go Advanced, select the code and click '#'. You'll then see the code tags added so that the code is formatted properly when displayed.
    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)

  14. #14
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: CPU at 100% when using AfxBeginThread

    Are you creating multiple instances of the CSerialCom? I assume you are. I'm also assuming that the CSerialCom class wraps some underlying functionality that interacts with the serial port.

    I'm wondering if this underlying functionality is thread safe (not sure what you are using, but some of the implementations aren't thread safe).

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

    Re: CPU at 100% when using AfxBeginThread

    Quote Originally Posted by Arjay View Post
    Are you creating multiple instances of the CSerialCom? I assume you are. I'm also assuming that the CSerialCom class wraps some underlying functionality that interacts with the serial port.

    I'm wondering if this underlying functionality is thread safe (not sure what you are using, but some of the implementations aren't thread safe).
    From the OP post #1, it fails on low end systems (Duo Core2, 5 GB Ram) but works on powerful systems (Core i7, quad core, 28GB Ram). Assuming this isn't a memory thrashing issue (memory usage is said by the OP to be stable), it seems that if the threads run on their own core then its ok but if threads share a core it doesn't.

    On the powerful system that works, what are the various core utilisations?
    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)

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)