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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:12:21+00:00 2026-06-10T17:12:21+00:00

In my Cocoa/iOS application I have a static double variable (let’s call it foo

  • 0

In my Cocoa/iOS application I have a static double variable (let’s call it foo) which must be:

  1. updated by one part of my app on a background thread
  2. read by another part of my app on the main thread

I’m looking for the most lightweight way to synchronize access to foo.

Note: I know that the simplest solution in these cases is usually to adjust the code such that only one thread accesses the variable, and therefore no synchronization is necessary. Let’s assume that I have determined that restricting foo access to a single thread is not possible in this case (maybe the action taken in step 1 occurs so rapidly/often that thread switching is not desirable). Please do not respond with an answer of “restrict foo access to one thread”. I am looking for alternatives to that.


My current understanding is that using a volatile variable is the most lightweight way to synchronize access to foo. So foo is declared something like this:

static volatile double foo = 60.0;

Background thread writing code is something like:

foo = 90.0;

Main thread reading code is something like:

double bar = 0.0;
bar = foo;
// use bar here …

Some questions:

  1. Is the main thread guaranteed to see the updated value of foo after it is written on the background thread (given that foo is declared volatile)? IOW, Have I done enough to ensure that the two threads will see the result each other’s read/write operations?
  2. I’m assuming that using volatile for a double value is faster/more-lightweight/preferable in this situation rather than a mutex lock like an @synchronized block or an NSLock. Is that true? IOW, is volatile the best solution for this particular case?
  • 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-10T17:12:23+00:00Added an answer on June 10, 2026 at 5:12 pm

    1) No. volatile is not a substitute for an atomic primitive here. Close, but not quite.

    2) Problem with volatile is, it’s not correct, as you now know.

    Ideal: You may have access to the latest atomic language features, such as _Atomic for C11 and/or <atomic> in C++11.

    If not, then you could either use a 64 bit integer and atomic functions, which you read by value, then transfer to a double, or you could use a simple pthread_mutex_t or perhaps even a spin lock (be very careful with spin locks).

    However… it may be better if you simply post changes to the value to the main thread’s run loop, if that is an option.

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

Sidebar

Related Questions

I have a view-based iOS cocoa touch application built and is running on iPhone.
Anyone have any idea about CASlotProxy Class in Cocoa (iOS SDK)? I found it
Assume I have a Cocoa-based Mac or iOS app. I'd like to run a
Given a Cocoa application which runs on Mac OS X 10.7 and later: What
I'm just a new baby in Cocoa. But i have some experience in iOS.
I have a WebView in a Cocoa application (macgap). ( NOTE I'm not talking
I am using XCode to develop a Cocoa touch application for the iOS platform
I encountered a weird behavior of Cocoa Touch / iOS involving static method. If
I need something like a tag(iOS) for cocoa NSWindow. I have an Error Message
I am writing an enterprise client/server application. The client is an iOS app which

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.