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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:20:15+00:00 2026-06-17T18:20:15+00:00

void f1(volatile int* ptr, int value) { *ptr = value; lock or DWORD PTR

  • 0
void f1(volatile int* ptr, int value)
{
    *ptr = value;
    lock or DWORD PTR [rsp], 0; // MemoryBarrier()
}


void f2(volatile int* ptr, int value)
{
    xchg DWORD PTR [ptr], value; // InterlockedExchange(ptr, value);
}

Equivalent in terms of semantics. Apparently xchg is locked whether or not the lock prefix is specified or not.

Edit: I’m using VS2010 currently but will probably port to VS2012 where I believe the compiler semantics regarding volatile have changed again.

  • 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-17T18:20:17+00:00Added an answer on June 17, 2026 at 6:20 pm

    Roughly yes. The lock or on the first case makes sure that the data has been updated before any other CPU can read any more memory, in the second case, the xchg instruction has an implicit lock, so all other processor (cores) will have to “release” their value of *ptr before your processor can update the value.

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

Sidebar

Related Questions

Consider this C code: extern volatile int hardware_reg; void f(const void *src, size_t len)
Here is my code: #include <stdio.h> #include <unistd.h> static volatile int t=0; int main(void){
We recently upgraded the code to gcc4.3.3 from gcc4.2.4 . void testAndSet( volatile int*
while calling this function: static inline void writel(unsigned int v, volatile void __iomem *addr)
I have a code snippet from here : volatile int volatileInt; int usualInt; void
- (void)TargetHit:(int)target{ void (^threadBlock)(void) = ^{ NSLog(@respond to selector %d, [self respondsToSelector:@selector(changeImageOfTarget:)]); [[NSOperationQueue mainQueue]
void foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size
void AFCQueue::ExtractValuesSecComplex(int startIndex, int endIndex,int helperIndex) { int size = 0,i,index; TimeType min_timestamp; bool
private const int Total = 500000; private static volatile int _count = 0; private
This is the second example of wikipedia SIGFPE page. #include <limits.h> int main(void) {

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.