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

Thread paused

Scheduled Pinned Locked Moved C / C++ / MFC
c++
13 Posts 5 Posters 1 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.
  • S susanne1

    I have a thread, it is has a normal priority, when it starts to excute, it runs for 5 or 6 seconds then stops for 3 or 4 seconds and run again, stops again this goes till it finishes his work. Any idea why this happens, programm code is in Visual C++. Thanks

    A Offline
    A Offline
    Adam Roderick J
    wrote on last edited by
    #3

    I prefer you to read chapter 2. Processes and threads from "Modern Operating Systems" by Andrew S Tannenbaum. It will be useful for you.

    Величие не Бога может быть недооценена.

    1 Reply Last reply
    0
    • CPalliniC CPallini

      Isn't that (being scheduled) the normal thread beahviour? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S Offline
      S Offline
      susanne1
      wrote on last edited by
      #4

      i changed the thread priority to HIGHEST, but it didi not help.

      CPalliniC 1 Reply Last reply
      0
      • S susanne1

        i changed the thread priority to HIGHEST, but it didi not help.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #5

        Simply don't do that. It is normal for threads being scheduled. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • CPalliniC CPallini

          Isn't that (being scheduled) the normal thread beahviour? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #6

          Being scheduled is normal thread behaviour! But it's not a normal thread behaviour to wait 3-5 seconds to get the next time slice!

          Greetings Covean

          CPalliniC 1 Reply Last reply
          0
          • S susanne1

            I have a thread, it is has a normal priority, when it starts to excute, it runs for 5 or 6 seconds then stops for 3 or 4 seconds and run again, stops again this goes till it finishes his work. Any idea why this happens, programm code is in Visual C++. Thanks

            C Offline
            C Offline
            Covean
            wrote on last edited by
            #7

            Can you please post your threads code to see if they are waiting for some reason. Edit: Did some typo.

            Greetings Covean

            S 1 Reply Last reply
            0
            • C Covean

              Can you please post your threads code to see if they are waiting for some reason. Edit: Did some typo.

              Greetings Covean

              S Offline
              S Offline
              susanne1
              wrote on last edited by
              #8

              void CMyThread::ThreadStart() { if (m_pSelThread != NULL) // CWinThread* m_pSelThread; { if (::WaitForSingleObject(m_pSelThread->m_hThread, 0) != WAIT_OBJECT_0) return; delete m_pSelThread; m_pSelThread = NULL; } m_pSelThread = AfxBeginThread((AFX_THREADPROC)ThreadFunction, this, THREAD_PRIORITY_NORMAL, 0,CREATE_SUSPENDED); m_pSelThread->m_bAutoDelete = FALSE; m_pSelThread->ResumeThread(); } UINT CMyThread::ThreadFunction ( LPVOID param ) { while(!bTerminate) // bool bTerminate { if ((::WaitForSingleObject(m_Sel_End_Event, 0) == WAIT_OBJECT_0)) { bTerminate = true; continue; } }

              C 1 Reply Last reply
              0
              • C Covean

                Being scheduled is normal thread behaviour! But it's not a normal thread behaviour to wait 3-5 seconds to get the next time slice!

                Greetings Covean

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                True but it really depends on how those 3-5 seconds come out? Are they just a OP impression? How did she measure them? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                C 1 Reply Last reply
                0
                • S susanne1

                  void CMyThread::ThreadStart() { if (m_pSelThread != NULL) // CWinThread* m_pSelThread; { if (::WaitForSingleObject(m_pSelThread->m_hThread, 0) != WAIT_OBJECT_0) return; delete m_pSelThread; m_pSelThread = NULL; } m_pSelThread = AfxBeginThread((AFX_THREADPROC)ThreadFunction, this, THREAD_PRIORITY_NORMAL, 0,CREATE_SUSPENDED); m_pSelThread->m_bAutoDelete = FALSE; m_pSelThread->ResumeThread(); } UINT CMyThread::ThreadFunction ( LPVOID param ) { while(!bTerminate) // bool bTerminate { if ((::WaitForSingleObject(m_Sel_End_Event, 0) == WAIT_OBJECT_0)) { bTerminate = true; continue; } }

                  C Offline
                  C Offline
                  Covean
                  wrote on last edited by
                  #10

                  I couldn't find any reason why your thread pauses for 3-5 seconds. But your "main loop" is a kind of resource-eating. Do you really meant to use a timeout of 0 to wait for this object? 0 timeout means to check the signaled state of the waitable handle and to return immediately. So your loop will iterate very very fast and consumes nearly 100% processing time.

                  UINT CMyThread::ThreadFunction(LPVOID param)
                  {
                  while(!bTerminate) // bool bTerminate
                  {
                  if ((::WaitForSingleObject(m_Sel_End_Event, 0) == WAIT_OBJECT_0))
                  {
                  bTerminate = true;
                  continue;
                  }
                  }
                  }

                  But as I said before this doesn't solve your problem. Is there maybe more code you can provide? By the way you also use the 0 timeout also in your ThreadStart function (but there it shouldn't be a problem).

                  Greetings Covean

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    True but it really depends on how those 3-5 seconds come out? Are they just a OP impression? How did she measure them? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #11

                    Yes there you are right. But in normal nobody should notice the thread-switches (if the thread and the process doesn't have the lowest priority or many real-time priority processes are running), so I would assume that the op has a real problem with it, no matter if the thread pauses for 300-500ms or 3-5s.

                    Greetings Covean

                    1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Isn't that (being scheduled) the normal thread beahviour? :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      X Offline
                      X Offline
                      xushih
                      wrote on last edited by
                      #12

                      use function Sleep(n_time).

                      CPalliniC 1 Reply Last reply
                      0
                      • X xushih

                        use function Sleep(n_time).

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #13

                        Uh?! OP missed? :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        In testa che avete, signor di Ceprano?

                        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