Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Hello,

I am trying to understand using NSThread.

I have made a sample application, where a button is provided to start the worker thread. I update label text in worker thread (not sure if this is the correct way to do this) and I want to stop the thread forcefully.

My stop thread is not working correctly, sometimes it is giving exception as well.

What is the correct way to stop a thread in between.

Attached zip of the code.
 

Attachments

  • ThreadTest 2.zip
    54.8 KB · Views: 90

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
1) Do all UI updates on the main thread. Otherwise they may not work. Use performSelectorOnMainThread:withObject:waitUntilDone: for this (or any other method you care to use to get it done in the correct thread).

2) What exceptions? These probably hint at the root cause

3) You cannot force-kill a NSThread. If you want to provide a way to terminate it have it check a variable every so often and set this from your UI when you want to cancel the operation.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hello,

I am trying to understand using NSThread.

I have made a sample application, where a button is provided to start the worker thread. I update label text in worker thread (not sure if this is the correct way to do this) and I want to stop the thread forcefully.

My stop thread is not working correctly, sometimes it is giving exception as well.

What is the correct way to stop a thread in between.

Attached zip of the code.

NSThread uses Posix threads, and you can't (safely) stop Posix threads from the outside. A Posix thread can only exit out of its own free will, either by running to its end, or by calling pthread_exit, or by calling the exit method of NSThread.
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
An NSThread can be "cancelled" by calling [myThread cancel]. However, this merely sets a flag requesting that the thread finish what it's doing, it doesn't kill the thread.

In order to support cancellation, your thread needs to periodically check [self isCancelled]. If it returns YES, your thread should perform any necessary cleanup and exit.
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
When I am trying to call cancel on my thread I am getting BAD_ACCESS error:

Code:
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.

My cancellation call is like:
Code:
[myThread cancel];

Any ideas why it is throwing this error?
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
When I am trying to call cancel on my thread I am getting BAD_ACCESS error:

Code:
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.

My cancellation call is like:
Code:
[myThread cancel];

Any ideas why it is throwing this error?

A stack trace would help. Your best bet is to poke around with the debugger and make sure you're not sending messages to objects that don't exist.
 

saurabhshukla

macrumors newbie
Mar 20, 2009
12
0
India
Supporting Answer

A stack trace would help. Your best bet is to poke around with the debugger and make sure you're not sending messages to objects that don't exist.

Yes, you are correct. “EXC_BAD_ACCESS” occurs only when you try to send message to objects that don't exist. so please debug your code & check out where its generating this error.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.