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 488 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.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on 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;
                }
    
    kshegunovK C 2 Replies Last reply
    0
    • Ronel_qtmasterR Offline
      Ronel_qtmasterR Offline
      Ronel_qtmaster
      wrote on 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
      • nicker playerN nicker player

        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;
                    }
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on 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
        • nicker playerN nicker player

          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 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

          • Login

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