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

  • SEARCH
  • Home
  • 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 329409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:35:08+00:00 2026-05-12T09:35:08+00:00

Seems these are the only methods to put a NSThread to sleep * sleepForTimeInterval:

  • 0

Seems these are the only methods to put a NSThread to sleep

* sleepForTimeInterval:

* sleepUntilDate:

Would it be bad practice what I am asking?

  • 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-05-12T09:35:09+00:00Added an answer on May 12, 2026 at 9:35 am

    Do you want your thread to just stop until some other thread tells it to start up again? If so, you could use an NSConditionLock. An NSConditionLock is similar to a condition variable. It has a couple of basic methods, lockWhenCondition, and unlockWithCondition, and lock. A typical usage is to have your background thread waiting on the condition lock with “lockWhenCondition:”, and the in you foreground thread to set the condition, which causes the background thread to wake up. The condition is a simple integer, usually an enumeration.

    Here’s an example:

    enum {
        kWorkTodo = 1,
        kNoWorkTodo = 0
    }
    
    - (id)init {
        if ((self = [super init])) {
            theConditionLock = [[NSCoditionLock alloc] initWithCondition: kNoWorkTodo];
            workItems = [[NSMutableArray alloc] init];
        }
    }
    
    - (void)startDoingWork {
        [NSThread detachNewThreadSelector:@selector(doBackgroundWork) toTarget:self withObject:nil];
    }
    
    - (void)doBackgroundWork:(id)arg {
        while (YES) {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            NSArray *items = nil;
            [theConditionLock lockWhenCondition:kWorkTodo]; // Wait until there is work to do
            items = [NSArray arrayWithArray:workItems]
            [workItems removeAllObjects];
            [theConditionLock unlockWithCondition:kNoWorkTodo];
            for(id item in items) {
                // Do some work on item.
            }
            [pool drain];
        }
    }
    
    - (void)notifyBackgroundThreadAboutNewWork {
        [theConditionLock lock];
        [workItems addObject:/* some unit of work */];
        [theConditionLock unlockWithCondition:kWorkTodo];
    }
    

    In this example, when startDoingWork is called doBackgroundWork: will start on a background thread, but then stop because there isn’t any work to do. Once notifyBackgroundThreadAboutNewWork is called, then doBackgroundWork: will fire up and process the new work, and then go back to sleep waiting for new work to be available, which will happen the next time notifyBackgroundThreadAboutNewWork is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that these two operators are pretty much the same - is there
The general consensus these days seems to be that you do not store binary
It seems like buttons and links are interchangeable these days. From a user-experience perspective,
I have these two methods and Java can't find the return in getNumEmails(). All
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Seems there's nothing to do… using the combo prototype/lowpro, no problems at all with
It seems there are many ways to do this, however, none of them make
It seems there are no free* .NET performance profilers that can profile on a
I am trying to read command lien argument but it seems there is some
My app has a registered shutdown function and it seems there's some issues with

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.