Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unstable Thread runs!

Unstable Thread runs!

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mostafa_pasha
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • M mostafa_pasha

      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

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        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

        M Offline
        M Offline
        mostafa_pasha
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • M mostafa_pasha

          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

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            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

            M Offline
            M Offline
            mostafa_pasha
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • M mostafa_pasha

              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

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups