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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:01:15+00:00 2026-05-23T06:01:15+00:00

i am using atomic operation provided by SunOs in <sys/atomic.h> , which is void

  • 0

i am using atomic operation provided by SunOs in <sys/atomic.h> , which is
void *atomic_cas_ptr(volatile void *target, void *cmp, void *newval);

now to make is usable, i have to check whether old value returned by this function and passed by callee function cmp are same, if they are then operation is successful.
but i have certain doubt: as this function returns a void pointer to the old value let’s call it void *old and i’m passing void *cmp, then i need to compare these two old and cmp, so how i am going to compare these two ? and if while comparing *old got changed then what i am going to do ?
in essence what i want to do is to warp this function, inside another function which takes these three arguments and return either true or false, which indicate success or failure.

about the CAS, i read that it’s misnomer to call it lockfree operation, since it eventually takes lock at hardware ( lock at bus ), It’s right correct ? that’s why the CAS is costly operation .

  • 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-23T06:01:16+00:00Added an answer on May 23, 2026 at 6:01 am

    Possibly the function declaration confused you. This function does not return a pointer to the old value (of what?), but the old value from the memory pointed by target (which should really be a pointer to void*, i.e. void* volatile * target).

    Usually if a CAS primitive returns an old value rather than a bool, you check CAS success with something like this:

    void* atomic_ptr; // global atomically modified pointer
    
    void* oldval, newval, comparand; // local variables
    /* ... */
    oldval = atomic_cas_ptr( (void*)&atomic_ptr, /* note that address is taken */
                              comparand, newval );
    if( oldval == comparand ) {
        // success
    } else {
        // failure
    }
    

    So when you compare old_val and comparand, you work with local variables that do not change concurrently (while global atomic_ptr might be changed again), and you compare pointer values without dereferencing.

    The function you want should be like this:

    bool my_atomic_cas_ptr(volatile void* target, void* comparand, void* newval)
    {
        return (comparand == atomic_cas_ptr(target, comparand, newval));
    }
    

    Note that since in some algorithms the old value (the one before CAS) should be known, it’s better to have a CAS primitive returning the old value rather than a bool, as you can easily build the latter from the former while the opposite is more complex and inefficient (see the following code that tries to obtain the correct old value out of a MacOS CAS primitive that returns a bool).

    void* CAS(void* volatile* target, void* comparand, void* newval)
    {
        while( !OSAtomicCompareAndSwapPtr(comparand, newval, target) ) {
            void* snapshot = *target;
            if( snapshot!=comparand ) return snapshot;
        }
        return comparand;
    }
    

    As for CAS locking memory bus, it depends on hardware. It was true for old x86 processors, but in modern x86 systems it’s different. First, there is no central bus; it was replaced by AMD’s HyperTransport and Intel’s QuickPath Interconnect. Second, in recent CPU generations locked instructions are not all serialized (see some data showing that locked instructions on different memory addresses do not interfere). And finally, in the commonly accepted definition lock freedom is the guarantee of system-wide progress, not absence of serializing synchronization.

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

Sidebar

Related Questions

I am using Single data context per atomic operation approach while using Linq to
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Suppose I'm writing (assembly) code for a CPU whose only atomic operation is an
I'm using WinXP on clients and Win2003 on a server. I need do atomic
I understand the volatile keyword in Java can make the read/write operations of reference
using ASP.NET MVC, I have a Model, to which I'm attaching attributes so that
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the

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.