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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:15:44+00:00 2026-05-16T01:15:44+00:00

I have an array with 250k entities (with a size of 20 bytes) and

  • 0
  • I have an array with 250k entities (with a size of 20 bytes) and 4 threads.
  • Each thread modifies ~100k random entities (that means, you can’t predict which entities it will touch). It can modify the same entity multiple times.
  • Each entity gets modified up to ~10 times.
  • The modification takes ~10-6 seconds.
  • Each entity gets modified by multiple threads

The both last points are the most important facts. The 5th point implies that I need a mechanism to protect my entities from getting corrupted due to concurrent access/modification. The 4th point makes me worry whether classical locking mechanisms like mutexes, which block threads, create to much overhead considering the short timespan a lock would be acquired for.

I’ve come up with two ideas:

  • Using spinlocks to overcome the overhead (presupposing my assumption about the overhead is correct in the first place).
  • Giving each thread a local copy of the array it can modify without interruptions. After all threads have finished, merging all arrays into one. This is possible because I’m able to pick a winner, if there are multiple copies of an entity.

What do You recommend? Do You agree on one of my ideas or do You recommend something else? Does Your recommendation change if I change the numbers to?:

  • 1M entities
  • 8 threads
  • ~500k random accesses
  • ~100 modifications per entity

Please also point me towards implementations in C#/.Net. Thanks in advance.

Additional Informations
The entities are value types (structs). I can not afford to create new objects for each write operation – only modify existing primitives.

  • 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-16T01:15:44+00:00Added an answer on May 16, 2026 at 1:15 am

    As they say, there’s more than one way to skin a cat (though why anybody wants skinned cat is another question) 🙂

    With 250K objects and 4 threads, you’d have to guess that conflicts will be (relatively) rare. That doesn’t mean that we can ignore them, but it may affect how we look for them. Testing a critical section is very fast, unless there is actually a conflict. That means that it might be feasible to check a critical section for every transaction, in the knowledge that relatively few checks will take more that a few CPU ticks.

    Is it feasible to create 250K critical sections? Maybe, I’m not sure. You can create a very lightweight spinlock with:

    while (0 != ::InterlockedExchange(&nFlag, 1)) {};
    DoStuff();
    nFlag = 0;
    

    An alternate approach might to partition the dataset and have each thread work on a unique set of objects. That makes conflicts impossible so no locking is needed. Depending on the nature of the problem, you might achieve this by having each thread operate on a range of data, or possibly by operating a queue for each worker thread and have one or more scanning threads identify objects needing processing and pushing them onto the appropriate processing queue.

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

Sidebar

Related Questions

I have an array of numbers that can be 1-24. ($timelist) I want to
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Have an array of chars like char members[255]. How can I empty it completely
I have array that element is hash a = [{:history_date=>15/07/10}, {:open_price=>7.90}] I want to
I have array of keywords. How can I find if any of these keywords
I have array that need to sort by their appearance, as they are written
I have array with both image colour and image id values. I can echo
I have array that looks like this, Array ( [email_address] => Array ( [0]
I have array of Objects (let say that class name is Snap) Snap. Is
I have array of objects person (int age; String name;) . How can 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.