-3

Edited.

How to shutdown(to stop it forever) current thread in a class method?

Now, I have:

methodName() {
    std::unique_lock<std::mutex> locker(...);
    cv.wait(locker, [this]{return ready_ || finished_;});
    if (finished_) //need to terminate cur thread
    ...
}
pvl
  • 958
  • 1
  • 6
  • 12
  • 4
    shutdown or suspend? Those are two different things. And anyway, why not just returning? – Marco A. Oct 19 '14 at 18:02
  • I need to stop it forever(shut down?). It Is simplified variant of code. I need to stop thread outside ThreadFunction. – pvl Oct 19 '14 at 18:03
  • you will have to implement this yourself, or have a look at boost, they have something like this in their thread implementation – Creris Oct 19 '14 at 18:04
  • To stop a thread forever, cause it to return. You can use an atomic variable shared with the thread to signal its return, for example. – Galik Oct 19 '14 at 18:06
  • @Galik, It is simplified variant. In the real: some threads wait for conditional_variable in SomeFunc. When I have a singal in another thread(which not wait), I want to shutdown(no, not notify, shutdown) all waiting threads. How? – pvl Oct 19 '14 at 18:14
  • Instead of doing `while(true){}` You can use: `std::atomic done(false); while(!done){}`. Then if you want to kill the thread, before you wake it up with the condition variable set `done = true;`. Then the loop will end gracefully. – Galik Oct 19 '14 at 18:23
  • @Galik, ok, I am asked wrong. I am trying to write multithread class and I need to shutdown process in method. I can`t modificate ThreadFucntion. – pvl Oct 19 '14 at 18:30
  • Just `throw`. I assume you have a `try - catch` block in your main method of the thread (guarantees stack unwinding - http://stackoverflow.com/questions/2331316/what-is-stack-unwinding) – zahir Oct 19 '14 at 18:36
  • @zahir, it seems you read prev question(I have edit it). I can`t modificate main_thread_function; – pvl Oct 19 '14 at 18:39
  • @Creris, maybe you can give some concret link`s/examples? – pvl Oct 19 '14 at 18:46

1 Answers1

1

The question isn't that clear, but if you are working on windows, you cant shut any thread down using TerminateThread or if you want to close current thread, you could use CloseThread.

lisu
  • 2,213
  • 14
  • 22