Goal C: How to kill a thread in another thread in Objective-C?

I tried to configure an application with multiple threads. One of the threads acts as a background to receive some data transfer. Right now, this thread is automatically killing itself after completing this task. Somehow I need to kill this thread in another thread in order to immediately stop working. Is there an api or method for this?

+2
source share
6 answers

In short, you cannot. Or rather, you should not. Never, under any circumstances.

There is no way that thread A knows the exact state of thread B when A kills B. If B holds any locks either in the middle of a system call or calls the system structure when A kills it, then the resulting state of your application will be non-deterministic.

Actually - this will be somewhat deterministic in the sense that you are largely guaranteed that a failure will occur in the near future.


If you need to terminate thread B, you need to do this in a controlled way. The most common way is to have a flag cancelor method that can be set / called. stream B then needs to periodically check this flag or check whether the method has been called, clear everything it does, and then exit.

That is, you will have to change the logic in thread B to support this.

+11

bbum , . , . , , , .

, ?

- , , . .

- , , . , - .

+3

? ( - , .) , , , ?

0

, , , - this, .

0

pthread, "pthread_kill", , , , .

It is true that killing a thread is not a good option, if you are looking for some kind of fix for some kind of problem, then you can try with that.

0
source

In my personal view, it is best to let the thread work fine. It is hard to give guarantees about trying to kill a thread.

0
source

Source: https://habr.com/ru/post/1774197/


All Articles