-
January 12th, 2015, 03:00 PM
#1
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?
-
January 12th, 2015, 04:28 PM
#2
Re: CPU at 100% when using AfxBeginThread
How many threads are you starting? What is the thread priority for each thread?
-
January 13th, 2015, 08:10 AM
#3
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.
-
January 13th, 2015, 12:57 PM
#4
Re: CPU at 100% when using AfxBeginThread
Originally Posted by OReubens
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.
-
January 13th, 2015, 01:39 PM
#5
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 13th, 2015, 02:18 PM
#6
Re: CPU at 100% when using AfxBeginThread
LOL. Took me a while to see the difference.
-
January 13th, 2015, 02:46 PM
#7
Re: CPU at 100% when using AfxBeginThread
Originally Posted by Arjay
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.
-
January 13th, 2015, 02:49 PM
#8
Re: CPU at 100% when using AfxBeginThread
Originally Posted by 2kaud
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%.
-
January 13th, 2015, 03:37 PM
#9
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 13th, 2015, 03:51 PM
#10
Re: CPU at 100% when using AfxBeginThread
Originally Posted by 2kaud
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.
-
January 13th, 2015, 05:13 PM
#11
Re: CPU at 100% when using AfxBeginThread
Originally Posted by WildWill
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
-
January 13th, 2015, 05:17 PM
#12
Re: CPU at 100% when using AfxBeginThread
Originally Posted by VictorN
It would help a lot if you added the code tags around the code snippets.
Not sure what you mean by "code tags"?
-
January 13th, 2015, 05:40 PM
#13
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 13th, 2015, 07:48 PM
#14
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).
-
January 14th, 2015, 06:41 AM
#15
Re: CPU at 100% when using AfxBeginThread
Originally Posted by Arjay
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++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|