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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:29:46+00:00 2026-05-25T12:29:46+00:00

I am writing a bit of code that implements an (unsigned) integer counter. It

  • 0

I am writing a bit of code that implements an (unsigned) integer counter.

  1. It is used from an arbitrary number of threads.
  2. A thread should get a unique value every time until overflow.
  3. If integer type range is overflown, zero should be returned.
  4. I have at my disposal atomic increment function (returns old value), and atomic compare-and-swap function.

All scenarios I have come up with so far suffer from race conditions on overflow. Is it possible to implement the counter with these constraints?

  • 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-25T12:29:46+00:00Added an answer on May 25, 2026 at 12:29 pm

    You can build everything from compare-and-swap. The general algorithm is

    int DoSomeAtomicCalculation()
    {
        static int value;
        int capture, newValue;
        do {
            capture = value;
            newValue = some_function_based_on(capture);
        } while (!compare_and_swap(value, capture, newValue));
        return newValue;
    }
    

    (I’m assuming that compare_and_swap takes a variable, a comparison value, and a swap value, and it returns true if the comparison succeeded (and the swap occurred).

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

Sidebar

Related Questions

I'm writing a bit of JavaScript code which should select a random item from
I'm am writing a bit of python code that gets data from a website.
I'm having issues with a bit of code that I am writing in C#.
So I inherited a bit of code that's waiting for communication from a network
I'm writing a bit of code that I want to populate a TreeView, which
I am writing a bit of code that appends my site nav with and
I'm writing a bit of code that will open a MessageBox on a separate
I am writing bit of code that relies on XslTransformation of one submodule. Since
I'm writing a bit of code to display a bar (or line) graph in
I'm writing a bit of JNI code where a DLL running in the process

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.