user268743
user268743

Reputation: 343

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

I was trying to set up a multi thread app. One of the threads is working as background to get some data transfer. Right now this thread automatically kill itself after it's job done. Somehow I need to kill this thread in another thread in order stop its job immediately. Are there any api or method for making this happen?

Upvotes: 2

Views: 7453

Answers (6)

Ken
Ken

Reputation: 31161

In my personal view it is best to let a thread run its course naturally. It's difficult to make guarantees about the effect of trying to kill a thread.

Upvotes: 0

bbum
bbum

Reputation: 162712

In short, you can't. Or, more precisely, you should not. Not ever and not under any circumstances.

There is absolutely no way for thread A to know the exact state of thread B when A kills B. If B is holding any locks or in the middle of a system call or calling into a system framework when A kills it, then the resulting state of your application is going to be nondeterministic.

Actually -- it will be somewhat deterministic in that you are pretty much guaranteed that a crash will happen sometime in the near future.


If you need to terminate thread B, you need to do so in a controlled fashion. The most common way is to have a cancel flag or method that can be set/called. thread B then needs to periodically check this flag or check to see if the method has been called, clean up whatever it is doing, and then exit.

That is, you are going to have to modify the logic in thread B to support this.

Upvotes: 11

Girish Kolari
Girish Kolari

Reputation: 2515

If you are using pthread then you try with 'pthread_kill' , I had tried long back it did not worked for me, basically if the thread is in some blocking call it won't work.

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

Upvotes: 0

benzado
benzado

Reputation: 84338

bbum is correct, you don't want to simply kill a thread. You can more safely kill a process, because it is isolated from the rest of the system. Because a thread shares memory and resources with the rest of the process, killing it would likely lead to all sorts of problems.

So, what are you supposed to do?

The only correct way of handling this is to have a way for your main thread to send a message to the worker thread telling it to quit. The worker thread must check for this message periodically and voluntarily quit.

An easy way to do this is with a flag, a boolean variable accessible by both threads. If you have multiple worker threads, you might need something more sophisticated, though.

Upvotes: 3

Mike
Mike

Reputation: 24964

One thing you could do would be pass messages between the front thread and the background thread, potentially using something like this to facilitate message passing.

Upvotes: 0

newacct
newacct

Reputation: 122449

Isn't that a bad idea? (If the other thread is in the middle of doing something in a critical section, it could leave stuff in an inconsistent state.) Couldn't you just set some shared flag variable, and have the other thread check it periodically to see if it should stop?

Upvotes: 0

Related Questions