Stop Thread query

Discussion in 'Mac Programming' started by McBgnr, Apr 20, 2009.

  1. McBgnr macrumors regular

    Joined:
    Apr 13, 2009
    #1
    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.
     

    Attached Files:

  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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.
     
  4. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #4
    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.
     
  5. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #5
    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?
     
  6. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #6
    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.
     
  7. saurabhshukla macrumors newbie

    saurabhshukla

    Joined:
    Mar 20, 2009
    Location:
    India
    #7
    Supporting Answer

    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.
     

Share This Page