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 3234190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:21:46+00:00 2026-05-17T17:21:46+00:00

I need to guarantee that the same thread performs various actions at arbitrary times.

  • 0

I need to guarantee that the same thread performs various actions at arbitrary times. First the thread needs to initialize a library, then I want the thread to sleep until work needs to be done and upon user input, I need to be able to pass selectors or blocks for execution.

How can I setup an NSRunLoop to sleep after initialization? After which, how do I signal the run loop to wake up and do something?

I’ve tried reading the Threading Programming Guide for iOS, but I’d like to avoid setting up classes as custom input classes and use something more lightweight like performSelector:onThread:

Can I set a timer to fire forever from now so the run loop doesn’t end?

Here’s essentially what I want in pseudo-code:

// Initialization Code...

do {
    sleepUntilSignaled();
    doWorkSentToThisThread();
while (!done);

Where I send the work to do as a performSelector:onThread: message. It would be even better if I could send the run loop a block like: ^{[someObj message]; [otherObj otherMsg];} but I’d be happy with performSelector since I’m pretty sure that’s possible without much extra coding.

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-05-17T17:21:46+00:00Added an answer on May 17, 2026 at 5:21 pm

    You have all the necessary pieces together in your question. You start your thread and have it run it’s runloop. If you need the thread to do something you can use performSelector:onThread: on the main thread to do it.

    There is one thing with the runloop you have to be aware though: It won’t run unless it has an input source or a timer attached to it. Just attach a timer to the run loop that fires some time in the distant future and you’re all set.

    // Initialization code here
    
    [NSTimer scheduledTimerWithTimeInterval: FLT_MAX
                                     target: self selector: @selector(doNothing:)
                                   userInfo: nil repeats:YES];
    
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    do {
        [rl runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } while (!done);
    

    Using performSelector:onThread:withObject: you can also pass your block to the background thread. All you need to do is to write a method somewhere that takes an block as a parameter and runs it:

    @interface NSThread (sendBlockToBackground)
    - (void) performBlock: (void (^)())block;
    @end
    
    @implementation NSThread (sendBlockToBackground)
    - (void) performBlock: (void (^)())block;
    {
        [self performSelector: @selector(runBlock:) 
                     onThread: self withObject: block waitUntilDone: NO];
    }
    
    - (void) runBlock: (void (^)())block;
    {
        block();
    }
    @end
    

    But maybe you should use a dispatch queue instead of all this. This requires less code and probably has less overhead also:

    dispatch_queue_t myQueue = dispatch_queue_create( "net.example.product.queue", NULL );
    dispatch_async( myQueue, ^{
        // Initialization code here
    } );
    
    // Submit block:
    dispatch_async( myQueue, ^{
        [someObject someMethod: someParameter];
    } );
    

    A dispatch queue created using dispatch_queue_create is a serial queue – all blocks sent to it will be performed in the same order they arrived, one after another.

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

Sidebar

Related Questions

I have a column that I need to be able to guarantee never gets
IEnumerable does not guarantee that enumerating twice will yield the same result. In fact,
Need help with a query that I wrote: I have three tables Company id
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I have a SQL Server table full of orders that my program needs to
Java memory model gives DRF guarantee(Data race freedom) which means that a data race
In my application I need to delete file with sensitive information. For that I'm
For a system I need to convert a pointer to a long then the
Let's say I release a code library as a standalone PHP class. Someone then
*I'm using Java. I have this thread, agent, that explores a room to determine

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.