How exit a thread?
-
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; }
-
It is better to use QObject::deleteLater() with QThread::finished().
so something like this
connect(&m_thread, &QThread::finished, this, &QObject::deleteLater); -
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; }
@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? -
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; }
@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.