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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:49:07+00:00 2026-05-11T09:49:07+00:00

I have some status data that I want to cache from a database. Any

  • 0

I have some status data that I want to cache from a database. Any of several threads may modify the status data. After the data is modified it will be written to the database. The database writes will always be done in series by the underlying database access layer which queues database operations in a different process so I cam not concerned about race conditions for those.

Is it a problem to just modify the static data from several threads? In theory it is possible that modifications are implemented as read, modify, write but in practice I can’t imagine that this is so.

My data handling class will look something like this:

class StatusCache { public:     static void SetActivityStarted(bool activityStarted)         { m_activityStarted = activityStarted; WriteToDB(); }     static void SetActivityComplete(bool activityComplete);         { m_activityComplete = activityComplete; WriteToDB(); }     static void SetProcessReady(bool processReady);         { m_processReady = processReady; WriteToDB(); }     static void SetProcessPending(bool processPending);         { m_processPending = processPending; WriteToDB(); } private:     static void WriteToDB(); // will write all the class data to the db (multiple requests will happen in series)     static bool m_activityStarted;     static bool m_activityComplete;     static bool m_processReady;     static bool m_processPending; }; 

I don’t want to use locks as there are already a couple of locks in this part of the app and adding more will increase the possibility of deadlocks.

It doesn’t matter if there is some overlap between 2 threads in the database update, e.g.

thread 1                        thread 2                    activity started in db SetActivityStarted(true)        SetActivityStarted(false)      m_activityStated = true                                   m_activityStarted = false                                   WriteToDB()               false   WriteToDB()                                               false 

So the db shows the status that was most recently set by the m_… = x lines. This is OK.

Is this a reasonable approach to use or is there a better way of doing it?

[Edited to state that I only care about the last status – order is unimportant]

  • 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-11T09:49:07+00:00Added an answer on May 11, 2026 at 9:49 am

    No, it’s not safe.

    The code generated that does the writing to m_activityStarted and the others may be atomic, but that is not garantueed. Also, in your setters you do two things: set a boolean and make a call. That is definately not atomic.

    You’re better off synchronizing here using a lock of some sort.

    For example, one thread may call the first function, and before that thread goes into ‘WriteDB()’ another thread may call another function and go into WriteDB() without the first going there. Then, perhaps the status is written in the DB in the wrong order.

    If you’re worried about deadlocks then you should revise your whole concurrency strategy.

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

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • 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
  • Editorial Team
    Editorial Team added an answer Nope. To quote Will Dean from 85122 you can't meaningfully… May 11, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer If you don't want to cast, Convert.ToInt32() could do the… May 11, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer WPF provides DynamicResources for just this reason. StaticResources - which… May 11, 2026 at 6:06 pm

Related Questions

Most projects have some sort of data that are essentially static between releases and
Is it possible (and supported cross-browser) to embed an image into the XML of
I'm programming an online game for two reasons, one to familiarize myself with server/client
We have a DLL that monitors changes in status and looks for events from

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.