Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:09:06+00:00 2026-06-01T02:09:06+00:00

I have an nsthread, a while loop within this one. It’s getting objects from

  • 0

I have an nsthread, a while loop within this one. It’s getting objects from a “thread-safe” queue in the main method. When I leave the UIViewController, which contains this nsthread object, I call the nsthread cancel method, but it doesn’t stop, because it’s locked by the “queueLock” NSCondition. When I go back to this UIViewController a new nsthread will be created and gets the objects form the queue, but the previous thread still exits and both of them try to use the same object from the queue, and this causes a memory management problem. My question is how should I stop this thread, when I leave the UIViewController.

NSThread main method:

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while ([self isCancelled] == NO) {
    RenderRequest *renderRequest = [queue get];
    [self doRender:renderRequest];
    [renderRequest release];    
}

[pool drain];

This is the get method of the queue class:

- (id) get {
    id toRet = nil;
    [queueLock lock];
    @try {
       while ([queueContents count] == 0) {
            [queueLock wait];
        }

        toRet = [queueContents lastObject];
        [queueContents removeLastObject];
    }

    @finally {
         [queueLock unlock];
         return toRet;
    }
}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:09:08+00:00Added an answer on June 1, 2026 at 2:09 am

    I wrote a simple demo, hope this can help you 🙂

    demo.h

    #import <Foundation/Foundation.h>
    
    @interface test : NSObject
    {
        NSCondition *queueCondition;
        NSThread *queueThread;
    
        NSMutableArray *queueTask;
    
        NSTimer *timer;
    }
    - (id)init;
    @end
    

    demo.m

    #import "demo.h"
    
    @interface demo (PrivateMethods)
    - (void)threadTest;
    - (void)cancelThread;
    - (void)addTask;
    @end
    
    
    @implementation demo
    
    - (id)init
    {
        self = [super init];
        if (self) {
            if (!queueThread) {
                if (!queueCondition) {
                    queueCondition = [[NSCondition alloc] init];
                }
    
                if (!queueTask) {
                    queueTask = [[NSMutableArray alloc] initWithCapacity:5];
                }
    
                queueThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadTest) object:nil];
                [queueThread start];
    
                [self performSelector:@selector(cancelThread) withObject:nil afterDelay:10];
    
                if (!timer) {
                    timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addTask) userInfo:nil repeats:YES] retain];
                }
            }
        }
        return self;
    }
    
    - (void)dealloc
    {
        [queueThread release];
        [queueCondition release];
        [queueTask release];
        [timer invalidate];
        [timer release];
        [super dealloc];
    }
    
    - (void)threadTest
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        while (![[NSThread currentThread] isCancelled]) {
            [queueCondition lock];
            [queueCondition wait];
    
            if ([queueTask count] == 0) {
                [queueCondition unlock];
                continue;
            }
    
            NSString *str = nil;
            while ((str = [queueTask lastObject])) {
                NSLog(@"getTask: %@", [queueTask lastObject]);
                [queueTask removeLastObject];
    
            }
    
            [queueCondition unlock];
        }
        NSLog(@"threadTest end");
        [pool drain];
    }
    
    - (void)addTask
    {
        [queueCondition lock];
        if (!queueTask) {
            queueTask = [[NSMutableArray alloc] initWithCapacity:5];
        }
        [queueTask addObject:@"new task"];
        [queueCondition signal];
        NSLog(@"add: new task");
        [queueCondition unlock];
    }
    
    - (void)cancelThread
    {
        [timer invalidate];
    
        [queueThread cancel];
    
        [queueCondition lock];
        [queueCondition signal];
        [queueCondition unlock];
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method -(void)myMethod:(MyObject*)obj And I am detaching a new thread [NSThread detachNewThreadSelector:@selector(myMethod)
I have a project in which a series of NSThread objects do something and
I have a strange problem while saving number of big images (from camera) to
I have a multicast receive daemon that is running as a NSThread. While it's
I have an issue while working with NSTimer. Let's assume I have this architecture
HI guys, ==>in my application i have used three different thread code for one
Im trying to create a NSThread game loop, i have some of the time
I have a method that performs a loop on a separate thread. For reasons
I have a NSThread that i would like to timeout after a certain amount
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.