Unstable Thread runs!
-
I run a worker thread like this: m_first = AfxBeginThread(MyThreadProcTT1, segmentation,THREAD_PRIORITY_ABOVE_NORMAL); in the MyThreadProcTT1, i run four worker threads like this: CWinThread* first=NULL; first = AfxBeginThread(MyThreadProc, pNewObject,THREAD_PRIORITY_ABOVE_NORMAL); second = AfxBeginThread(MyThreadProc1, pNewObject1,THREAD_PRIORITY_ABOVE_NORMAL); third = AfxBeginThread(MyThreadProc2, pNewObject2,THREAD_PRIORITY_ABOVE_NORMAL); forth = AfxBeginThread(MyThreadProc3, pNewObject3,THREAD_PRIORITY_ABOVE_NORMAL); WaitForSingleObject(first->m_hThread, INFINITE); WaitForSingleObject(second->m_hThread, INFINITE); WaitForSingleObject(third->m_hThread, INFINITE); WaitForSingleObject(forth->m_hThread, INFINITE); in most of the time, the program work fine. But in some situations, the program halts and do not anything! Is there anyone can help me! Best, MJM
-
I run a worker thread like this: m_first = AfxBeginThread(MyThreadProcTT1, segmentation,THREAD_PRIORITY_ABOVE_NORMAL); in the MyThreadProcTT1, i run four worker threads like this: CWinThread* first=NULL; first = AfxBeginThread(MyThreadProc, pNewObject,THREAD_PRIORITY_ABOVE_NORMAL); second = AfxBeginThread(MyThreadProc1, pNewObject1,THREAD_PRIORITY_ABOVE_NORMAL); third = AfxBeginThread(MyThreadProc2, pNewObject2,THREAD_PRIORITY_ABOVE_NORMAL); forth = AfxBeginThread(MyThreadProc3, pNewObject3,THREAD_PRIORITY_ABOVE_NORMAL); WaitForSingleObject(first->m_hThread, INFINITE); WaitForSingleObject(second->m_hThread, INFINITE); WaitForSingleObject(third->m_hThread, INFINITE); WaitForSingleObject(forth->m_hThread, INFINITE); in most of the time, the program work fine. But in some situations, the program halts and do not anything! Is there anyone can help me! Best, MJM
I spot an issue in your code: All threads have their priorities set to
THREAD_PRIORITY_ABOVE_NORMAL
, which I think could be avoided. What is your justification for doing this?mostafa_pasha wrote:
But in some situations, the program halts and do not anything!
And what does that mean? Have you used the debugger to step into the code? Have you verified all the WaitForSingleObject calls have returned (as you're asking the program to wait for an
INFINITE
amount of time)?!“Follow your bliss.” – Joseph Campbell
-
I spot an issue in your code: All threads have their priorities set to
THREAD_PRIORITY_ABOVE_NORMAL
, which I think could be avoided. What is your justification for doing this?mostafa_pasha wrote:
But in some situations, the program halts and do not anything!
And what does that mean? Have you used the debugger to step into the code? Have you verified all the WaitForSingleObject calls have returned (as you're asking the program to wait for an
INFINITE
amount of time)?!“Follow your bliss.” – Joseph Campbell
I taught coping the software is for thread priority.So i tried to change the priority to ABOVE_NORMAL. About debugging, i changed the code: WinThread* first=NULL; first = AfxBeginThread(MyThreadProcT, &pmom1,THREAD_PRIORITY_ABOVE_NORMAL); if(first == NULL) { AfxMessageBox(_T("Can not run a thread!!!")); return; } CWinThread* second=NULL; second = AfxBeginThread(MyThreadProcT1, &pmom2,THREAD_PRIORITY_ABOVE_NORMAL); if(second == NULL) { AfxMessageBox(_T("Can not run a thread!!!")); return; } if (WaitForSingleObject(first->m_hThread, INFINITE) == WAIT_FAILED) { ErrorExit(TEXT("Thread_First")); // show the GetLastError } if (WaitForSingleObject(second->m_hThread, INFINITE)) { ErrorExit(TEXT("Thread_Second")); // show the GetLastError } I caught this message box: Thread_Second failed with error 6: The Handle is invalid!!! Does it mean second thread finish before the the first thread? Best MJM
-
I taught coping the software is for thread priority.So i tried to change the priority to ABOVE_NORMAL. About debugging, i changed the code: WinThread* first=NULL; first = AfxBeginThread(MyThreadProcT, &pmom1,THREAD_PRIORITY_ABOVE_NORMAL); if(first == NULL) { AfxMessageBox(_T("Can not run a thread!!!")); return; } CWinThread* second=NULL; second = AfxBeginThread(MyThreadProcT1, &pmom2,THREAD_PRIORITY_ABOVE_NORMAL); if(second == NULL) { AfxMessageBox(_T("Can not run a thread!!!")); return; } if (WaitForSingleObject(first->m_hThread, INFINITE) == WAIT_FAILED) { ErrorExit(TEXT("Thread_First")); // show the GetLastError } if (WaitForSingleObject(second->m_hThread, INFINITE)) { ErrorExit(TEXT("Thread_Second")); // show the GetLastError } I caught this message box: Thread_Second failed with error 6: The Handle is invalid!!! Does it mean second thread finish before the the first thread? Best MJM
mostafa_pasha wrote:
Thread_Second failed with error 6: The Handle is invalid!!!
It could mean that the thread was not even started. Remember that these operations are completely asynchronous. The code has reached the WFSO call does not mean the thread creation function called above that has successfully spawned the thread by then. I'd strongly recommend that you get your basics right. Read this essay by Dr. Joseph Newcomer, which covers many aspects: Worker Threads[^]
“Follow your bliss.” – Joseph Campbell
-
mostafa_pasha wrote:
Thread_Second failed with error 6: The Handle is invalid!!!
It could mean that the thread was not even started. Remember that these operations are completely asynchronous. The code has reached the WFSO call does not mean the thread creation function called above that has successfully spawned the thread by then. I'd strongly recommend that you get your basics right. Read this essay by Dr. Joseph Newcomer, which covers many aspects: Worker Threads[^]
“Follow your bliss.” – Joseph Campbell
Unfortunately, I used SetEvent , .... in the debug mode, everything is OK. in the release mode, it seems that the threads can not start!!! Best, MJM
-
Unfortunately, I used SetEvent , .... in the debug mode, everything is OK. in the release mode, it seems that the threads can not start!!! Best, MJM
mostafa_pasha wrote:
Unfortunately, I used SetEvent , .... in the debug mode, everything is OK. in the release mode, it seems that the threads can not start!!!
Please read this: Debug vs Release[^]
“Follow your bliss.” – Joseph Campbell