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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:56:46+00:00 2026-05-27T03:56:46+00:00

Windows API offers InterlockedExchange , which sets a value in memory atomically. Using only

  • 0

Windows API offers InterlockedExchange, which sets a value in memory atomically. Using only GCC intrinsics, I’d like to create an equivalent of that function. Would setting the value and then calling a memory barrier be sufficient (see the code below) ?

template <typename T>
T InterlockedExchange(volatile T& _data, T _value)
{
    const T oldValue = _data;
    _data = _value;
    __sync_synchronize();
    return oldValue;
}

Thank you.

EDIT: The proposed snippet is NOT a correct solution to the problem, as it is clearly not atomic (but, well, I had to give a try at least).

  • 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-27T03:56:47+00:00Added an answer on May 27, 2026 at 3:56 am

    Use __sync_val_compare_and_swap __sync_lock_test_and_set, not __sync_synchronize.

    This has exactly the same function as InterlockedExchange.

    Something like this (untested code!):

    template<typename T> T InterlockedExchange(T& data, T& new_val)
    {
        return __sync_lock_test_and_set(&data, new_val);
    }
    

    EDIT:
    Oi, I read wrong, you wanted InterlockedExchange, not InterlockedCompareExchange … so that is __sync_lock_test_and_set (the name is a misleading Intel-nomer, but it’s exactly what you want).
    See here, bottom of the page.

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

Sidebar

Related Questions

Using the Windows API, how can I get a list of domains on my
Is there a Windows API or any way to determine on which physical processor/core
I want to learn windows API and PInvoke methods just like MesseageBeep in User32.dll.
I am trying to use native windows API with Qt using mingw toolset. There
I'm using Windows API's CreateFile and SetCommState functions to open a number of serial
My Windows XP/7 program launches a child process using the Windows API CreateProcess() function
I am developing a Windows API application without using MFC . I am using
I have created a Windows GUI program using C and the Windows API, and
I'm working with the iTunes COM API on Windows. I'd like to manage the
I'm using the Windows API SendInput() call to simulate keyboard events. The following snippet

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.