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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:54:02+00:00 2026-05-11T13:54:02+00:00

I am maintaining a carbon C++ application on OS X. There is a spin

  • 0

I am maintaining a carbon C++ application on OS X. There is a ‘spin lock’ in one of the objects in the code that looks like this,

while ( mState != RELEASED ) {     sleep( 1 ); } 

There is a post render maintenance callback that is fed to AudioUnitAddRenderNotify() that would set the mState of these various objects.

I was thinking about using the built-in OSSpinLock family of functions to replace this (if nothing else because they are thread safe and the above code doesn’t seem to be).

Would this be a reasonable equivalent? Should I also add some lock protection in the maintenance thread which manipulates the mState of these objects?

OSSpinLock spin_lock = OS_SPINLOCK_INIT;  if (!OSSpinLockTry(&spin_lock))     OSSpinLockLock(&spin_lock);  while (mState != RELEASED)     OSSpinLockTry(&spin_lock);  OSSpinLockUnlock(&spin_lock); 
  • 1 1 Answer
  • 2 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. 2026-05-11T13:54:03+00:00Added an answer on May 11, 2026 at 1:54 pm

    Don’t use a spinlock — spinlocks waste CPU cycles, looping endlessly until they get the lock. Spinlocks should only be used when the time that the lock will be held will be very short.

    A better alternative would be to use a condition variable, such as the ones available in the pthreads library. This way, your thread will sleep and not waste any CPU cycles until the condition is satisfied. See the example listed in that link for how to use pthreads condition variables.

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

Sidebar

Related Questions

While maintaining some code, I discovered that we have an infinite hang-up in a
I'm currently maintaining an application and noticed that many of the objects that have
I am maintaining a web application that uses java.protocol.handler. I initialize it with a
While maintaining an old product, I came across an error that results in the
I recently started maintaining an old EJB2 application running on OC4J. That includes EJB
I am working on maintaining someone else's code that is using multithreading, via two
I'm maintaining a legacy site and on this site there is a weird mouseover
I am working on maintaining a ASP.NET MVC application that has the following coding
I'm maintaining an application that was copy-pasted for each new customer ( ...yeah, I
I'm maintaining code for a mathematical algorithm that came from a book, with references

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.