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

  • Home
  • SEARCH
  • 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 106831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:39:31+00:00 2026-05-11T01:39:31+00:00

I was worrying about race conditions in an application I’m designing, when I was

  • 0

I was worrying about race conditions in an application I’m designing, when I was wondering about this question.

Let’s say I have a large array or collection of some sort that is managed by one component of my program, let’s call that component Monitor. Its job is to regularly check if the collection is ‘dirty’, i. e. has changed recently, and if so, write a snapshot to disk (this is to checkpoint the application in case a crash occurs) and mark it as clean again.

Other components of the same program, running in a different thread, call the Monitor’s methods to add data to or modify data in the array/collection. Those methods mark the collection as dirty.

Now, the altering methods run in the other components’ threads, right? And if I’m not so lucky, they might be called while the snapshot is written to disk, change data that has already been written, set the dirty flag and the Monitor’s thread unsets it just after that, without having saved the changes (it was already past the element when it changed). So I have a dirty collection that’s marked as clean.

For a while, I thought that I could maybe solve the problem by making a temporary copy of the collection, mark it clean and then go and serialize the copy. But would the copying be atomic, i. e. can I be sure that the collection will not change while I’m copying it?

Meanwhile, I think I have found better solutions, like

  • setting a lock flag before beginning the write to disk and make the data altering methods wait until the flag is unset
  • have the data altering methods write to a ‘change queue’ instead of directly to the collection and have the thread that does the disk writing process that queue

and I think the lock flag might be the best way to go. But I’m still curious: Is copying a variable atomic?


Follow-up: Maybe this should go in a question of its own, but actually it’s very much the same. According to the answers below, my ‘lock flag’ approach might also not work, right? Because the data altering method might check the lock flag while it is being set to the ‘locked’ value and decide it’s not locked. So I need a special construction like a mutex if I really want to do this right, correct?


Kudos to erickson for his very helpful answer on my follow-up. I really should have made this two questions so I could have accepted two answers. Please, vote him up, too.

  • 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. 2026-05-11T01:39:32+00:00Added an answer on May 11, 2026 at 1:39 am

    No. For instance, long variables in Java are not atomic on 32-bit machines.

    In addition, there’s a ‘thread caching’ issue – unless your variable is volatile or inside synchronized block, another thread may not see the change to variable value. This is true for all types of variables, not just long.

    Read here: http://gee.cs.oswego.edu/dl/cpj/jmm.html, especially ‘atomicity’ and ‘visibility’ paragraphs.

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

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer At the point you're trying to define slots, the class… May 11, 2026 at 11:20 am
  • added an answer It's a variadic method, meaning it takes a variable number… May 11, 2026 at 11:20 am
  • added an answer I'm guessing you don't want to take a commission off… May 11, 2026 at 11:20 am

Related Questions

I was worrying about race conditions in an application I'm designing, when I was
I was trying to get my head around XAML and thought that I would
I was reading this question about how to parse URLs out of web pages
I was just looking through some information about Google's protocol buffers data interchange format.
I was recently tasked with coming up with an offsite backup strategy. We have
I was working with quality yesterday doing some formal testing. In their procedure they
I was using a CASE called MAGIC for a system I'm developing, I've never
I was writing some ASP.NET control when I came to the scenario where I
I was trying to add a favicon to a website earlier and looked for
I was looking at the API documentation for stl vector, and noticed there was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.