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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:30:04+00:00 2026-06-13T03:30:04+00:00

I am just starting to look into multi-threaded programming and thread safety. I am

  • 0

I am just starting to look into multi-threaded programming and thread safety. I am familiar with busy-waiting and after a bit of research I am now familiar with the theory behind spin locks, so I thought I would have a look at OSSpinLock’s implementation on the Mac. It boils down to the following function (defined in objc-os.h):

static inline void ARRSpinLockLock(ARRSpinLock *l)
{
again:
   /* ... Busy-waiting ... */
    thread_switch(THREAD_NULL, SWITCH_OPTION_DEPRESS, 1);
    goto again;
}

(Full implementation here)

After doing a bit of digging, I now have an approximate idea of what thread_switch‘s parameters do (this site is where I found it). My interpretation of what I have read is that this particular call to thread_switch will switch to the next available thread, and decrease the current thread’s priority to an absolute minimum for 1 cycle. ‘Eventually’ (in CPU time) this thread will become active again and immediately execute the goto again; instruction which starts the busy-waiting all over again.

My question though, is why is this call actually necessary? I found another implementation of a spin-lock (for Windows this time) here and it doesn’t include a (Windows-equivalent) thread switching call at all.

  • 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-13T03:30:05+00:00Added an answer on June 13, 2026 at 3:30 am

    I actually don’t think they’re that different. In the first case:

    static inline void ARRSpinLockLock(ARRSpinLock *l)
    {
        unsigned y;
    again:
        if (__builtin_expect(__sync_lock_test_and_set(l, 1), 0) == 0) {
            return;
        }
        for (y = 1000; y; y--) {
    #if defined(__i386__) || defined(__x86_64__)
            asm("pause");
    #endif
            if (*l == 0) goto again;
        }
        thread_switch(THREAD_NULL, SWITCH_OPTION_DEPRESS, 1);
        goto again;
    }
    

    We try to acquire the lock. If that fails, we spin in the for loop and if it’s become available in the meantime we immediately try to reacquire it, if not we relinquish the CPU.

    In the other case:

    inline void Enter(void)
    {
        int prev_s;
        do
        {
            prev_s = TestAndSet(&m_s, 0);
            if (m_s == 0 && prev_s == 1)
            {
                break;
            }
            // reluinquish current timeslice (can only
            // be used when OS available and
            // we do NOT want to 'spin')
            // HWSleep(0);
        }
        while (true);
    }
    

    Note the comment below the if, which actually says that we could either spin or relinquish the CPU if the OS gives us that option. In fact the second example seems to just leave that part up to the programmer [insert your preferred way of continuing the code here], so in a sense it’s not a complete implementation like the first one.

    My take on the whole thing, and I’m commenting on the first snippet, is that they’re trying to achieve a balance between being able to get the lock fast (within 1000 iterations) and not hogging the CPU too much (hence we eventually switch if the lock does not become available).

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

Sidebar

Related Questions

I'm just starting to look into the world of Functional Reactive Programming in Haskell,
I'm just starting to look into using LINQ for my database (and XML, and
I starting too look at a bit of security into my site. My site
Just starting to look into WCF and came across the WSDualHttpBinding binding. I have
I'm just starting to look into Futures and the ScheduledExecutorService in Java, and I'm
Just starting to look into using LinqDataSource for a GridView and I’m looking for
I'm just starting out refactoring my code into multiple assemblies, and feel a bit
I'm just starting to get into backbone.js. It looks like it's pretty involved and
I'm just starting to get into JQuery so I apologize in advance if this
I'm just starting to dive into some basic Android development and have been experimenting

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.