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. Problem with thread

Problem with thread

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 4 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.
  • M Offline
    M Offline
    Majid Shahabfar
    wrote on last edited by
    #1

    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.

    I 1 Reply Last reply
    0
    • M Majid Shahabfar

      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.

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      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.

      B S 2 Replies Last reply
      0
      • I Iain Clarke Warrior Programmer

        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.

        B Offline
        B Offline
        Brad Sokol
        wrote on last edited by
        #3

        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

        I 1 Reply Last reply
        0
        • B Brad Sokol

          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

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            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.

            S Offline
            S Offline
            Scott H Settlemier
            wrote on last edited by
            #5

            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.

            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