views:

451

answers:

6

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?

A: 

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?

newacct
+6  A: 

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.

bbum
I got it. Thanks for your recommendation. Is there any way that I can terminate thread B as soon as possible while thread A want it to do so?
A: 

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.

Mike
Thanks for your recommendations. That helps.
+2  A: 

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.

benzado
I tried to add a boolean flag to let thread terminate itself by exit(0). But due to unfamiliar with multithread deployment in Iphone, I've no idea whether the boolean flag work in time. Is there any idea to make this happen immediately?
Don't call `exit()`, that will kill the whole process. Just let the main function of the thread end naturally, by calling `return`.If you need it to respond to a terminate request more quickly, the worker thread must check the flag more often.
benzado
A: 

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.

Girish Kolari