Atomic operations require support from hardware?










0















Recently when I was reading about lock-less programming, I came across 'Atomic operations'. I started digging deep into it. All links explain how to write atomic operations and their usages.



However, I am looking for some details on atomic operations.



  1. Do atomic operations need any hardware capabilities?

  2. Do languages provide APIs for it? if yes, how are atomic APIs implemented?

  3. Are these limited only to kernel space programming, or are they available for user-space programming too?









share|improve this question



















  • 1





    If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

    – Mad Physicist
    Nov 16 '18 at 6:11







  • 1





    Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

    – Hans Passant
    Nov 16 '18 at 8:41















0















Recently when I was reading about lock-less programming, I came across 'Atomic operations'. I started digging deep into it. All links explain how to write atomic operations and their usages.



However, I am looking for some details on atomic operations.



  1. Do atomic operations need any hardware capabilities?

  2. Do languages provide APIs for it? if yes, how are atomic APIs implemented?

  3. Are these limited only to kernel space programming, or are they available for user-space programming too?









share|improve this question



















  • 1





    If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

    – Mad Physicist
    Nov 16 '18 at 6:11







  • 1





    Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

    – Hans Passant
    Nov 16 '18 at 8:41













0












0








0








Recently when I was reading about lock-less programming, I came across 'Atomic operations'. I started digging deep into it. All links explain how to write atomic operations and their usages.



However, I am looking for some details on atomic operations.



  1. Do atomic operations need any hardware capabilities?

  2. Do languages provide APIs for it? if yes, how are atomic APIs implemented?

  3. Are these limited only to kernel space programming, or are they available for user-space programming too?









share|improve this question
















Recently when I was reading about lock-less programming, I came across 'Atomic operations'. I started digging deep into it. All links explain how to write atomic operations and their usages.



However, I am looking for some details on atomic operations.



  1. Do atomic operations need any hardware capabilities?

  2. Do languages provide APIs for it? if yes, how are atomic APIs implemented?

  3. Are these limited only to kernel space programming, or are they available for user-space programming too?






c++ c atomic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:08









Jonathan Leffler

573k946861038




573k946861038










asked Nov 16 '18 at 6:08









user3691131user3691131

613




613







  • 1





    If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

    – Mad Physicist
    Nov 16 '18 at 6:11







  • 1





    Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

    – Hans Passant
    Nov 16 '18 at 8:41












  • 1





    If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

    – Mad Physicist
    Nov 16 '18 at 6:11







  • 1





    Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

    – Hans Passant
    Nov 16 '18 at 8:41







1




1





If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

– Mad Physicist
Nov 16 '18 at 6:11






If I'm not mistaken, the short answer is proper locking, so #1 is a no: en.m.wikipedia.org/wiki/Dekker%27s_algorithm, en.m.wikipedia.org/wiki/Peterson%27s_algorithm. Hardware support makes everything easier if course

– Mad Physicist
Nov 16 '18 at 6:11





1




1





Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

– Hans Passant
Nov 16 '18 at 8:41





Yes. On x86 and x64 this kind of "lockless" code is usually implemented with the LOCK instruction prefix :) Not an api, the compiler's back-end knows about the intrinsics.

– Hans Passant
Nov 16 '18 at 8:41












2 Answers
2






active

oldest

votes


















1















Do atomic operations need any hardware capabilities?




Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.




Do languages provide APIs for it? if yes, how are atomic APIs implemented?




Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic




Are these limited only to kernel space programming, or are they available for user-space programming too?




Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.



The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list






share|improve this answer






























    0















    Does atomic operation need any hardware capabilities.




    In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).



    See also this and that answers of mine (to questions similar to yours).






    share|improve this answer




















    • 1





      Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

      – Mad Physicist
      Nov 16 '18 at 6:14






    • 2





      @MadPhysicist But if you had an infinite number of monkeys...

      – user4581301
      Nov 16 '18 at 6:15






    • 1





      @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

      – Mad Physicist
      Nov 16 '18 at 6:26







    • 1





      But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

      – Lundin
      Nov 16 '18 at 9:35










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332338%2fatomic-operations-require-support-from-hardware%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    Do atomic operations need any hardware capabilities?




    Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.




    Do languages provide APIs for it? if yes, how are atomic APIs implemented?




    Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic




    Are these limited only to kernel space programming, or are they available for user-space programming too?




    Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.



    The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list






    share|improve this answer



























      1















      Do atomic operations need any hardware capabilities?




      Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.




      Do languages provide APIs for it? if yes, how are atomic APIs implemented?




      Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic




      Are these limited only to kernel space programming, or are they available for user-space programming too?




      Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.



      The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list






      share|improve this answer

























        1












        1








        1








        Do atomic operations need any hardware capabilities?




        Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.




        Do languages provide APIs for it? if yes, how are atomic APIs implemented?




        Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic




        Are these limited only to kernel space programming, or are they available for user-space programming too?




        Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.



        The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list






        share|improve this answer














        Do atomic operations need any hardware capabilities?




        Sure, CPUs guarantee that some of their instructions are atomic. Some of those instructions are "special", i.e. differ from other instructions (prefixed, or have other mnemonics), but some instructions might be "normal". For example, aligned stores and loads are guaranteed to be atomic on most CPUs.




        Do languages provide APIs for it? if yes, how are atomic APIs implemented?




        Sure, have a look for example at C++ implementation: https://en.cppreference.com/w/cpp/atomic/atomic




        Are these limited only to kernel space programming, or are they available for user-space programming too?




        Sure, those instructions do not require any privileges, so they are available for userspace. There is a variety of libraries and data structures which leverage atomic operations.



        The keywords for the search are "lockless" or "non-blocking". Here is an example: https://en.wikipedia.org/wiki/Non-blocking_linked_list







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 18:30









        Andriy BerestovskyyAndriy Berestovskyy

        4,6812522




        4,6812522























            0















            Does atomic operation need any hardware capabilities.




            In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).



            See also this and that answers of mine (to questions similar to yours).






            share|improve this answer




















            • 1





              Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

              – Mad Physicist
              Nov 16 '18 at 6:14






            • 2





              @MadPhysicist But if you had an infinite number of monkeys...

              – user4581301
              Nov 16 '18 at 6:15






            • 1





              @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

              – Mad Physicist
              Nov 16 '18 at 6:26







            • 1





              But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

              – Lundin
              Nov 16 '18 at 9:35















            0















            Does atomic operation need any hardware capabilities.




            In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).



            See also this and that answers of mine (to questions similar to yours).






            share|improve this answer




















            • 1





              Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

              – Mad Physicist
              Nov 16 '18 at 6:14






            • 2





              @MadPhysicist But if you had an infinite number of monkeys...

              – user4581301
              Nov 16 '18 at 6:15






            • 1





              @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

              – Mad Physicist
              Nov 16 '18 at 6:26







            • 1





              But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

              – Lundin
              Nov 16 '18 at 9:35













            0












            0








            0








            Does atomic operation need any hardware capabilities.




            In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).



            See also this and that answers of mine (to questions similar to yours).






            share|improve this answer
















            Does atomic operation need any hardware capabilities.




            In practice, yes. In principle, the C++ (read n3337) or C (read n1570) standards don't even require a computer like the one we are using (you could, in theory and unethically, use a bunch of human slaves instead; a nicer variant of that is a teacher using the students of his classroom to "run" a tiny C or C++ program; this is a very good way of teaching programming).



            See also this and that answers of mine (to questions similar to yours).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 6:16

























            answered Nov 16 '18 at 6:12









            Basile StarynkevitchBasile Starynkevitch

            179k13174375




            179k13174375







            • 1





              Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

              – Mad Physicist
              Nov 16 '18 at 6:14






            • 2





              @MadPhysicist But if you had an infinite number of monkeys...

              – user4581301
              Nov 16 '18 at 6:15






            • 1





              @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

              – Mad Physicist
              Nov 16 '18 at 6:26







            • 1





              But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

              – Lundin
              Nov 16 '18 at 9:35












            • 1





              Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

              – Mad Physicist
              Nov 16 '18 at 6:14






            • 2





              @MadPhysicist But if you had an infinite number of monkeys...

              – user4581301
              Nov 16 '18 at 6:15






            • 1





              @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

              – Mad Physicist
              Nov 16 '18 at 6:26







            • 1





              But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

              – Lundin
              Nov 16 '18 at 9:35







            1




            1





            Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

            – Mad Physicist
            Nov 16 '18 at 6:14





            Interesting how that was the first alternative to a computer that came to your mind. But on second thought, I couldn't find anything better that isn't essentially derivative :)

            – Mad Physicist
            Nov 16 '18 at 6:14




            2




            2





            @MadPhysicist But if you had an infinite number of monkeys...

            – user4581301
            Nov 16 '18 at 6:15





            @MadPhysicist But if you had an infinite number of monkeys...

            – user4581301
            Nov 16 '18 at 6:15




            1




            1





            @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

            – Mad Physicist
            Nov 16 '18 at 6:26






            @user4581301 ...trained to meticulously execute machine instructions, sure. I feel like you'd need underpaid humans to teach the monkeys though :)

            – Mad Physicist
            Nov 16 '18 at 6:26





            1




            1





            But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

            – Lundin
            Nov 16 '18 at 9:35





            But if the monkeys can only carry one byte at a time, they might not be able to move a word atomically. You will need a mechanism to ensure that no monkey carries in a new ls byte, while another monkey is busy moving the ms byte. For example a mechanism the gives the approaching monkey a banana if it patiently waits for the first monkey to carry away both bytes, before dropping a new one. Btw, are monkeys big or little endian?

            – Lundin
            Nov 16 '18 at 9:35

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332338%2fatomic-operations-require-support-from-hardware%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric