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. How exit a thread?
Forum Updated to NodeBB v4.3 + New Features

How exit a thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 451 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.
  • N Offline
    N Offline
    nicker player
    wrote on 13 Feb 2024, 13:54 last edited by
    #1

    My application depends on some dlls what like the oxorm,cefview,cef and so on.
    I created a new std::thread which used the third party dll, and detached it.
    When I called the qApp->exit() or qApp->quit();
    the main process of the application disappeared,but the dll of the third party came to the back process.So how to finish the detached thread?
    I used the code while(condition) to exit the while loop.but the thread is still running.
    And the codes below seemed useless.

                if((*it)->joinable()){
                    (*it)->join();
                }
                else{
                    pthread_t tid = (*it)->native_handle();
                    pthread_cancel(tid);
                    delete *it;
                }
    
    K C 2 Replies Last reply 13 Feb 2024, 23:17
    0
    • R Offline
      R Offline
      Ronel_qtmaster
      wrote on 13 Feb 2024, 14:08 last edited by Ronel_qtmaster
      #2

      It is better to use QObject::deleteLater() with QThread::finished().
      so something like this
      connect(&m_thread, &QThread::finished, this, &QObject::deleteLater);

      1 Reply Last reply
      0
      • N nicker player
        13 Feb 2024, 13:54

        My application depends on some dlls what like the oxorm,cefview,cef and so on.
        I created a new std::thread which used the third party dll, and detached it.
        When I called the qApp->exit() or qApp->quit();
        the main process of the application disappeared,but the dll of the third party came to the back process.So how to finish the detached thread?
        I used the code while(condition) to exit the while loop.but the thread is still running.
        And the codes below seemed useless.

                    if((*it)->joinable()){
                        (*it)->join();
                    }
                    else{
                        pthread_t tid = (*it)->native_handle();
                        pthread_cancel(tid);
                        delete *it;
                    }
        
        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 13 Feb 2024, 23:17 last edited by kshegunov
        #3

        @nicker-player said in How exit a thread?:

        I created a new std::thread which used the third party dll, and detached it.

        If you have detached it then you can no longer control it. If you can't control it, you can't wait for it to finish.

        My application depends on some dlls what like the oxorm,cefview,cef and so on.

        I don't know what these are, but most libraries don't need to be run in a dedicated thread. Some are even actively discouraging it.
        So to point out the elephant in the room - why did you decide you need to start multiple threads?

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • N nicker player
          13 Feb 2024, 13:54

          My application depends on some dlls what like the oxorm,cefview,cef and so on.
          I created a new std::thread which used the third party dll, and detached it.
          When I called the qApp->exit() or qApp->quit();
          the main process of the application disappeared,but the dll of the third party came to the back process.So how to finish the detached thread?
          I used the code while(condition) to exit the while loop.but the thread is still running.
          And the codes below seemed useless.

                      if((*it)->joinable()){
                          (*it)->join();
                      }
                      else{
                          pthread_t tid = (*it)->native_handle();
                          pthread_cancel(tid);
                          delete *it;
                      }
          
          C Offline
          C Offline
          ChrisW67
          wrote on 13 Feb 2024, 23:18 last edited by ChrisW67
          #4

          @nicker-player said in How exit a thread?:

          And the codes below seemed useless.

          Not that this has anything to do with Qt, but what was useless about it?

          If you try to std::thread::join() a thread then that thread needs to cleanly terminate while you wait. What triggers/signals that thread to terminate is entirely up to you.

          If you try to pthread_cancel() a native thread then how it reacts is dependent on the characteristics of that native thread.

          Edit: Had not seen that the threads were detached. These threads will not be joinable.

          1 Reply Last reply
          1

          1/4

          13 Feb 2024, 13:54

          • Login

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