Problem with thread
-
Hi dear, I created a thread. But when I start the thread CPU load become 100%. I use these code.
void CEventLine::BeginThread() { m_isRunning = TRUE; AfxBeginThread(ThreadStart,this,THREAD_PRIORITY_LOWEST); } UINT CEventLine::ThreadStart(LPVOID pParam) { CEventLine* pEventLine = (CEventLine*)pParam; if (pEventLine == NULL || !pEventLine->IsKindOf(RUNTIME_CLASS(CEventLine))) return 1; // if pEventLine is not valid while(m_isRunning == 1) pEventLine->Start(); return 0; // thread completed successfully } void CEventLine::Start() { // some code }
CEventLine is a CDialog based class. Thank you in advance. -
Hi dear, I created a thread. But when I start the thread CPU load become 100%. I use these code.
void CEventLine::BeginThread() { m_isRunning = TRUE; AfxBeginThread(ThreadStart,this,THREAD_PRIORITY_LOWEST); } UINT CEventLine::ThreadStart(LPVOID pParam) { CEventLine* pEventLine = (CEventLine*)pParam; if (pEventLine == NULL || !pEventLine->IsKindOf(RUNTIME_CLASS(CEventLine))) return 1; // if pEventLine is not valid while(m_isRunning == 1) pEventLine->Start(); return 0; // thread completed successfully } void CEventLine::Start() { // some code }
CEventLine is a CDialog based class. Thank you in advance.It's no great shock that the CPU load is 100%. After all...
while (m_isRunning == 1)
pEventLine->Start ();
...
}void CEventLine::Start ()
{
}simply calls Start again and again and again... with no pause for "breath". It would be the same if you put this code in your main thread. You have a few choices... a) Put up with it. Assuming this is only for a brief duration. b) Put up with it, but make it less of a nuisance. I've not used MFCs threads, and MFC is poor at sharing, but you can lower thread priority to make other threads more responsice.
DWORD dwID;
hScanThread = ::CreateThread (NULL, 0, DoScan, this, CREATE_SUSPENDED, &dwID);
if (hScanThread == NULL)
{
DWORD dwError = ::GetLastError ();
} else
::SetThreadPriority (hScanThread, THREAD_PRIORITY_HIGHEST);c) Put a delay (sleep) between each iteration of the loop. This would have to be short. d) Some other tactic depending on the contents of
CEventLine::Start ()
. Iain. -
It's no great shock that the CPU load is 100%. After all...
while (m_isRunning == 1)
pEventLine->Start ();
...
}void CEventLine::Start ()
{
}simply calls Start again and again and again... with no pause for "breath". It would be the same if you put this code in your main thread. You have a few choices... a) Put up with it. Assuming this is only for a brief duration. b) Put up with it, but make it less of a nuisance. I've not used MFCs threads, and MFC is poor at sharing, but you can lower thread priority to make other threads more responsice.
DWORD dwID;
hScanThread = ::CreateThread (NULL, 0, DoScan, this, CREATE_SUSPENDED, &dwID);
if (hScanThread == NULL)
{
DWORD dwError = ::GetLastError ();
} else
::SetThreadPriority (hScanThread, THREAD_PRIORITY_HIGHEST);c) Put a delay (sleep) between each iteration of the loop. This would have to be short. d) Some other tactic depending on the contents of
CEventLine::Start ()
. Iain.Choice e) might be let the thread naturally relinquish the CPU by blocking on an I/O or message queue, for example. Depending on how often/long it blocks, this may be enough. Brad
-
Choice e) might be let the thread naturally relinquish the CPU by blocking on an I/O or message queue, for example. Depending on how often/long it blocks, this may be enough. Brad
True, but then it may not respond to the "quit" flag. Then again, waiting on multiple objects (the first of which is a KILL, DIE, DIE! event) is the way I usually go. But with less dramatic variable names. Iain
-
It's no great shock that the CPU load is 100%. After all...
while (m_isRunning == 1)
pEventLine->Start ();
...
}void CEventLine::Start ()
{
}simply calls Start again and again and again... with no pause for "breath". It would be the same if you put this code in your main thread. You have a few choices... a) Put up with it. Assuming this is only for a brief duration. b) Put up with it, but make it less of a nuisance. I've not used MFCs threads, and MFC is poor at sharing, but you can lower thread priority to make other threads more responsice.
DWORD dwID;
hScanThread = ::CreateThread (NULL, 0, DoScan, this, CREATE_SUSPENDED, &dwID);
if (hScanThread == NULL)
{
DWORD dwError = ::GetLastError ();
} else
::SetThreadPriority (hScanThread, THREAD_PRIORITY_HIGHEST);c) Put a delay (sleep) between each iteration of the loop. This would have to be short. d) Some other tactic depending on the contents of
CEventLine::Start ()
. Iain.Sleep(0) is perfect here. You can be nice to the system by simply using your time slice to make *one* check of the flag and then turning over control to another thread. Although typically I would argue against using a flag and using an appropriate synchronization object instead.