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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:39:19+00:00 2026-05-31T12:39:19+00:00

I need to read/write 16 bytes atomically. I do the writing only using cmpxchg16,

  • 0

I need to read/write 16 bytes atomically. I do the writing only using cmpxchg16, which is available on all x64 processors except I think for one obscure AMD one.

Now the question is for aligned 16 byte values, only ever modified using cmpxchg16 (which acts like a full memory barrier) is it ever possible to read a 16 byte location that’s half old data and half new data?

As long as I read with a SSE instruction (so the thread cannot be interrupted in the middle of the read) I think that it’s impossible (even in multiprocessor numa systems) for the read to see inconsistent data. I think it must be atomic.

I am making the assumption that when cmpxchg16 is executed, it modifies the 16 bytes atomically, not by writing two 8 byte blocks with the potential for other threads to do a read in between (honestly I don’t see how it could work if it wasn’t atomic.)

Am I right? If I’m wrong, is there a way to do an atomic 16 byte read without resorting to locking?

Note: There are a couple similar questions here but they don’t deal with the case where the writes are done only with cmpxchg16, so I feel this is a seperate, unanswered question.

Edit: Actually I think my reasoning was faulty. An SSE load instruction may be executed as two 64bit reads, and it may be possible for the cmpxchg16 to be executed in between the two reads by another processor.

  • 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-31T12:39:20+00:00Added an answer on May 31, 2026 at 12:39 pm
    typedef struct
    {
      unsigned __int128 value;
    } __attribute__ ((aligned (16))) atomic_uint128;
    
    unsigned __int128
    atomic_read_uint128 (atomic_uint128 *src)
    {
      unsigned __int128 result;
      asm volatile ("xor %%rax, %%rax;"
                    "xor %%rbx, %%rbx;"
                    "xor %%rcx, %%rcx;"
                    "xor %%rdx, %%rdx;"
                    "lock cmpxchg16b %1" : "=A"(result) : "m"(*src) : "rbx", "rcx");
      return result;
    }
    

    That should do the trick. The typedef ensures correct alignment. The cmpxchg16b needs the data to be aligned on a 16 byte boundary.

    The cmpxchg16b will test if *src contains a zero and write a zero if so (nop). In either case the correct value will stand in RAX:RDX afterwards.

    The code above evaluates to something as simple as

    push   %rbx
    xor    %rax,%rax
    xor    %rbx,%rbx
    xor    %rcx,%rcx
    xor    %rdx,%rdx
    lock cmpxchg16b (%rdi)
    pop    %rbx
    retq
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read from and write to Excel spreadsheets using Delphi 2010. Nothing
If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
I have a problem. I need to trace all read/write operations to the registry
I'm trying to upload a file via URLConnection, but I need to read/write it
I need to read and write octet streams to send over various networks to
I need to read and write some data through named pipes. I have tested
For a project I need to read and write ESRI shapefiles. It should be
I need to write a simple program for work that does the following: read
As the title says, I need to write a small program to read data
Sometimes we deploy applications behind customer firewall and we need read only access to

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.