Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ Thread consumes all CPUs
QtWS25 Last Chance

C++ Thread consumes all CPUs

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.0k Views
  • 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
    MohammadReza
    wrote on last edited by
    #1

    Hi friends.

    I have a function. In my function there is a c++ thread & a Qtimer. By c++ thread I receive ARP Reply packets & by QTimer I send ARP Request packets.

    The simplified structure:

    @
    int foo()
    {
    ... some codes ...

    QTimer::singleShot(1000, this, SLOT(beginSending()));
    
    std::thread tCapture(Capture);
    tCapture.join();
    
    return 0;
    

    }
    @

    @
    void Capture()
    {
    while ( ! finishCapturing )
    {
    do sth
    }
    }

    @

    In the tCapture thread I have a while loop that consumes all CPUs & the Qtimer does not work!
    I use .join() because I want to wait for the thread to finish.
    when I ser the 'finishCapturing ' flag in Qtimer slot, the thread will be finished.

    The above codes dont work correctly, because the c++ thread consumes all CPUs!

    what is the problem?

    Thanks a lot.
    Ya Ali.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      [quote]
      @
      while ( ! finishCapturing )
      {
      do sth
      }
      @
      [/quote]This loop will do sth as fast as it can go -- that's why it's consuming 100% CPU. You need to tell it to sleep() to stop it from consuming all your CPU time.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MohammadReza
        wrote on last edited by
        #3

        Hello dear JKSH,

        I had used sleep() but did not work!

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Why don't you use the better QThread? Or the other thread classes in Qt? There is ample documentation off it and works great without you have to worry about threading etc.
          "Threading!":http://qt-project.org/doc/qt-5/qtconcurrent-index.html

          Greetz, Jeroen

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MohammadReza
            wrote on last edited by
            #5

            Hello dear Jeroentje@home,

            Yes but as I said in my first post, I don't want the foo function finish before completing Capture thread & the slot.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mmoll
              wrote on last edited by
              #6

              If the new thread is spawned from the same thread that QTimer's message queue is processed in, it won't work. The call to join() blocks that thread indefinitely, so the "timer alarm" event is never processed and your thread is never stopped. You should always only join() a thread after signalling termination.

              edit: if QTimer's message queue is processed in another thread, it won't work either, as then you have an indirect connection to your timeout slot, which won't be processed.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="mmoll" date="1405594535"]If the new thread is spawned from the same thread that QTimer's message queue is processed in, it won't work. The call to join() blocks that thread indefinitely, so the "timer alarm" event is never processed and your thread is never stopped.[/quote]Good catch!

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved