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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:01:51+00:00 2026-06-11T19:01:51+00:00

I have a simple scenario where a NSTimer is scheduled on a background thread

  • 0

I have a simple scenario where a NSTimer is scheduled on a background thread to send a packet through a socket every so often. My main thread however, is responsible for destroying the timer and the socket.

The destruction looks like this:

if (self.connected) {
    [self.pingTimer invalidate];

    if (self.socket != -1) {
        close(self.socket);
        self.socket = -1;
    }

    self.connected = NO;
}

How can I ensure in Objective-C that once this section of code is entered, the scheduler isn’t allowed to suspend this thread until the code section is over? In other words I want to make this code section atomic.

My understanding is that the @synchronized directive is a high-level semaphore/mutex so to ensure atomicity of that code block, I would need to enclose the code block itself, as well as all the code which references either self.connected, self.pingTimer and self.socket in @synchronized directives. Am I correct in thinking this?

  • 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-11T19:01:52+00:00Added an answer on June 11, 2026 at 7:01 pm

    As far as I know there is no way to prevent the OS from preempting your thread and have another run in its place. The best you can do is synchronize the access. But in reading your code I infer the following:

    • connected is an internal state variable.
    • You want to avoid the timer being scheduled while your are disconnecting.
    • You want to avoid disconnecting while the timer is scheduled.

    I think its best to avoid synchronization whenever possible. Fortunately, you are using a NSTimer, which will make things trivial.

    NSTimer does NOT run on a background thread but on a NSRunLoop and probably on the main run loop just like your dealloc. Timers are event sources for the run loop. When they fire they enqueue an event, which in turn calls your code when it gets processed. Because run loops process one event at a time it means that the code in your scheduled timer and the code in your dealloc get fully executed before the other can run. It also means that you don’t need any special synchronization mechanism.

    - (void) dealloc
    {
       // ...
       if (_connected) {
           [_pingTimer invalidate];
           if (_socket != -1)
               close(_socket);
       }
       // ...
       [super dealloc];
    }
    

    Note: I prefer to use the state variables directly when I can unless the setters have other side effects. There is also no point in setting state variables in dealloc simply because the object will disapear once the method call is done.

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

Sidebar

Related Questions

I have a simple scenario, where two servers are connected through a gigabit link.
I have this simple scenario: main.m (which is my main class) myClass.m (which is
I have the following seemingly simple scenario, however I'm still pretty new to NHibernate.
I have a 'simple' scenario: Read some JSON file, Filter or change some of
I have a simple scenario where i want to output only json to the
I have a very simple scenario but poor experience in this area and googling
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have a very simple scenario involving a database and a JMS in an
I have a very simple scenario which I cannot get to work. I am

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.