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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:20:33+00:00 2026-05-14T03:20:33+00:00

My program has two threads: Main execution thread that handles user input and queues

  • 0

My program has two threads:

  1. Main execution thread that handles user input and queues up database writes
  2. A utility thread that wakes up every second and flushes the writes to the database

Inside the main thread, I occasionally need to make reads on the database. When this happens, performance is not important, but correctness is. (In a perfect world, I would be reading from a cache, not making a round-trip to the database – but let’s put that aside for the sake of discussion.)

How do I make sure that the main thread sees a correct / quiescent database?

A standard mutex won’t work, since I run the risk of having the main thread grab the mutex before the data gets flushed to the database. This would be a big race condition.

What I really want is some sort of mutex that lets the main thread of execution proceed only AFTER the mutex has been grabbed and released once. Does such a thing exist? What’s the best way to solve this problem?

UPDATE: After doing some additional research, I might use Boost’s Conditional Variable to address this problem. Either that, or just bite the bullet and cache my writes. Thanks for the feedback!

  • 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-14T03:20:33+00:00Added an answer on May 14, 2026 at 3:20 am

    If you don’t have more than one main execution thread (ie, the only thread that will push writes onto the worker thread is the same thread that will be reading from the database), then you can probably just have a simple “pending writes” variable/function that you can check before sending a read, and spinlock or wait for a signal until the writes have been flushed. It sounds like you won’t need to perform any locking or synchonization on the writes, if they can simply be queued up to be processed by the worker thread.

    Basically, as long as you are guaranteed that in between the check for that ‘pending writes’ state and when you actually perform the read, there are no writes, then you don’t need to do anything too fancy.

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

Sidebar

Related Questions

I have a program (time lapse maker) which has two threads that updates a
Is tight looping in a program bad? I have an application that has two
I'm trying to write an android application that has two main activities: login screen
My program has one dialog and two sockets. Both sockets are derived from CAsyncSocket,
The batch file has only two lines: c:\program.exe ... pause but pause does't run
My program has to use certain files that reside in another directory. Right now
I have a C++ program that has a pretty terrible memory leak, about 4MB
I have a minimal program that does little more than let me set two
I am writing a program that connects to a web host that has a
I have the main component of my program which runs in the main thread.

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.