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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:57+00:00 2026-05-26T00:46:57+00:00

For an assignment (it’s for concurrency if you wonder) – I have to implement

  • 0

For an assignment (it’s for concurrency if you wonder) – I have to implement my own lock

(more specific: a TaS, a TTas and an Array-Lock, as described in “The Art of Multiprocessor Programming”)

There are some test in- and output schemes online that I tried (too bad they take pretty long to try).

Your program is to count 9-digit numbers that pass a certain test

(it’s called the elfproef in dutch, I don’t know the english equivalence, sorry).

Sometimes I got a slightly different number, which suggests that my lock doesn’t work a 100% right.

I have implemented the locks like this:

interface Lock
{
    void Lock();
    void Unlock();
}

class TaSLock : Lock
{
    AtomicBool state = new AtomicBool(false);

    void Lock.Lock()
    { while (state.getAndSet(true)); }

    void Lock.Unlock()
    { state.getAndSet(false); }
}

The AtomicBool is implemented with an integer, because the Interlocked class doesn’t have operations for Boolean variables. This isn’t optimal in terms of memory usage but it doesn’t (or shouldn’t) matter for the speed.

class AtomicBool
{
    int value;
    static int True = 1, False = -1;

    public AtomicBool(bool value)
    {
        if (value) this.value = True;
        else this.value = False;
    }

    public void set(bool newValue)
    {
        if (newValue) Interlocked.Exchange(ref value, True);
        else Interlocked.Exchange(ref value, False);
    }

    public bool getAndSet(bool newValue)
    {
        int oldValue;
        if (newValue) oldValue = Interlocked.Exchange(ref value, True);
        else oldValue = Interlocked.Exchange(ref value, False);
        return (oldValue == True);
    }

    public bool get()
    {
        return (Interlocked.Add(ref value, 0) == 1);
    }
}

Now in the parallel part I have just used:

theLock.Lock();
counter++;
theLock.Unlock();

But each time I get slightly different results.

Is there something obvious I’m doing wrong?

  • 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-26T00:46:57+00:00Added an answer on May 26, 2026 at 12:46 am

    Hans is right. Your atomic get-and-set boolean appears to be correct — in fact, it appears to me to be somewhat over-engineered. And the lock appears to be correct as well, insofar as you’ve built yourself a potentially highly inefficient “spin lock”. (That is, all the waiting threads just sit there in a tight loop asking “can I go yet? can I go yet?” instead of going to sleep until it is their turn.)

    What is not correct is that your lock provides no guarantee whatsoever that any two threads that both have a view of “counter” have a consistent view of “counter”. Two threads could be on different processors, and those different processors could have different copies of “counter” in their caches. The cached copies will be updated, and only occasionally written back to main memory, thereby effectively “losing” some increases.

    The real implementation of locking in C# ensures that a full-fence memory barrier is imposed so that reads and writes cannot move “forwards and backwards in time” across the fence. That gives a hint to the processors that they need to not be so smart about caching “counter” so aggressively.

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

Sidebar

Related Questions

I have an assignment to implement my own version of Collections.fill() and Collections.reverse(). The
I have an assignment for my architecture class which to implement a 31 backgammon
I have a small assignment where I have to use a 2d array to
My assignment is to implement a multithreaded web server in Java but i have
for a programming assignment I have been asked to implement a solution to the
For a school assignment I have to write x86 assembly code, except I can't
For my university assignment I have to design some basic managment system for sicknesses
For a database assignment I have to model a system for a school. Part
For my assignment I have been assigned the task of creating a DTD for
For my university assignment I have to make a networkable version of pacman. I

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.