• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Thread deadlock?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Thread deadlock?


  • Subject: Thread deadlock?
  • From: Trygve Inda <email@hidden>
  • Date: Mon, 11 Aug 2008 12:09:05 +0000
  • Thread-topic: Thread deadlock?

I am seeing a deadlock I think... It works in the debugger, but hangs when
running alone. The killThread is called as part of the
applicationWillTerminate delegate method.

My thought is that somehow after blocking to wait for kConditionThreadIdle,
my performSelectorOnMainThread is getting called. When this happens, it gets
stuck because the main thread is already blocked.

Perhaps after calling if (![self threadMustExit]), but before dispatching to
the main thread?

Does anyone have any thoughts on this or a good way to prevent it?

Trygve

-(void)awakeFromNib
{
 lock = [[NSConditionLock alloc] initWithCondition:kConditionThreadRunning];

[NSThread detachNewThreadSelector:@selector(threadMethod:) toTarget:self
withObject:self];
}


- (void)threadMethod:(id)anObject
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    while (1)
    {
        if (![self threadMustExit])
            [self doStuff];

        if (![self threadMustExit])
            [self performSelectorOnMainThread:@selector(doThreadUnsafeStuff)
                  withObject:nil waitUntilDone:YES];

        if ([lock lockWhenCondition:kConditionThreadMustExit)
        {
            [lock unlock];
            break;
        }
    }

    [lock lock];
    [lock unlockWithCondition:kConditionThreadIdle];

    [pool release];
}


- (BOOL)threadMustExit
{
    [lock lock];
    BOOL threadMustExit = ([lock condition] == kConditionThreadMustExit);
    [lock unlock];

    return (threadMustExit);
}


-(void)killThread
{
    [lock lock];
    if ([lock condition] == kConditionThreadRunning)
    {
        //    instruct thread to exit
        [lock unlockWithCondition:kConditionThreadMustExit];

        //    Wait for thread to exit
        [lock lockWhenCondition:kConditionThreadIdle];
        [lock unlock];
    }
    else
        [lock unlock];
}




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Thread deadlock?
      • From: "Michael Ash" <email@hidden>
  • Prev by Date: Non-NSObject object and garbage collection
  • Next by Date: Re: Web links with PyObjC
  • Previous by thread: Re: Non-NSObject object and garbage collection
  • Next by thread: Re: Thread deadlock?
  • Index(es):
    • Date
    • Thread