My program has two threads:
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!
If multiple threads access the same resource for read and write, the value may not be the correct value. For example, let's say our application contains two threads, one thread for reading content from the file and another thread writing the content to the file.
Synchronizing data for multithreading. When multiple threads can make calls to the properties and methods of a single object, it is critical that those calls be synchronized. Otherwise one thread might interrupt what another thread is doing, and the object could be left in an invalid state.
Thread Synchronization is used to access the shared resources in a multithread environment. The programmer decides the situation for when to use the synchronization object efficiently. The MFC Thread Synchronization classes internally call the Win32 API functions.
Multiple threads are allowed to access the methods and fields, but only a single thread is allowed at any one time. Learn about .NET thread synchronization primitives used to synchronize access to a shared resource or control thread interaction See how unhandled exceptions are handled in .NET.
Several solutions are possible:
I don't see a problem with the second choice. What's the harm of flushing when there's nothing to flush?
Your proposed solution surely still results in a race condition, since a "write" may come in at any time, even halfway through a new event being queued.
A solution you might try is atomic database state value: you do your processing, then compare to an atomic state value to ensure that you are reading from a database that is in the state that is the same as when began the read. If it's different, you start over. This may be subject to starvation, but that's a separate issue.
Every time you change the database, you must compare and swap the atomic state value you used, indicating a change.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With