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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:29:18+00:00 2026-06-11T04:29:18+00:00

I am looking for information regarding the thread-safety of concurrent writes to the System.Collections.BitArray

  • 0

I am looking for information regarding the thread-safety of concurrent writes to the System.Collections.BitArray class.

Specifically, consider the following contrived example:

BitArray bits = new BitArray(1000000);

Parallel.For(0, bits.Count, i =>
{
    bits[i] = i % 3 == 0;
});

Instinct tells me that concurrent unlocked access would produce an incorrect result if two threads attempt to write to the same underlying integral value of the bitarray, yet I can not find any evidence to support it, and I have not encountered any problems during runtime.

Is this a safe operation?

If not, why am I not seeing this code fail or produce incorrect output?

Update

After further testing, I think the following test proves that using a BitArray in this example is not thread safe.

Using a bool[] on the other hand, does seem to be safe.

private static bool CompareBitArrays(BitArray a, BitArray b)
{
    if (a.Count != b.Count) return false;
    for (int i = 0; i < a.Count; i++)
    {
        if (a[i] != b[i]) return false;
    }
    return true;
}

static void Main(string[] args)
{
    int numElements = 1000000;

    //create single-threaded bitarray with certifiably correct values.
    BitArray controlGroup = new BitArray(numElements);
    for (int i = 0; i < numElements; i++)
    {
        controlGroup[i] = i % 3 == 0;
    }

    //create a BitArray and bool array of equal size and fill them using Parallel.For.
    BitArray bits = new BitArray(numElements);
    bool[] bools = new bool[numElements];

    Parallel.For(0, numElements, i =>
    {
        bits[i] = bools[i] = i % 3 == 0;
    });

    //Create a BitArray from the bool array
    BitArray boolBits = new BitArray(bools);

    //Check if they contain correct values
    bool isBitArrayCorrect = CompareBitArrays(controlGroup, bits); //FALSE
    bool isBoolArrayCorrect = CompareBitArrays(controlGroup, boolBits); //TRUE
}

As I mentioned, I suspect the reason to be that 32 values in the BitArray share the same integral value of the array.

Is this logic correct?

For the sake of question, please assume no threads other than those shown in the code are accessing the collection.

  • 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-11T04:29:20+00:00Added an answer on June 11, 2026 at 4:29 am

    Look into BitArray.Set method code:

    public void Set(int index, bool value)
    {
        if (index < 0 || index >= this.Length)
        {
            throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
        }
        if (value)
        {
            this.m_array[index / 32] |= 1 << index % 32;
        }
        else
        {
            this.m_array[index / 32] &= ~(1 << index % 32);
        }
        this._version++; // this is definitely thread-unsafe
    }
    

    As far as you accessing collection member by index, without enumerating it, the only thread-unsafe line of code I see there, is the last line this._version++;.

    But it is there, so, you can consider this code as thread-unsafe.

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

Sidebar

Related Questions

Unfortunately, Google didn't help me much: I'm looking for some information regarding the execution
After looking up information regarding loading offline resources into UIWebView, it looks like I'm
I'm looking for articles, forum or blog posts dealing with SharePoint and thread safety?
I'm looking for information regarding digital signatures for document signing (PDF, office, etc) What
I’m looking for some information regarding how I would create a demo period in
I'm looking for information regarding what Sizzle (jQuery) does internally when you run a
I'm looking for some information regarding using SQLITE with AIR in Flash CS4, I
I've been looking for some information regarding the scrapped ECMAScript 4th Edition without much
I'm having trouble finding any information regarding the following warning when linking a dynamic
Looking for information regarding the advantages and disadvantages of both fcntl and lockf for

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.