My program has two threads:
- The main thread of execution that processes user input and creates queues for writing to the database
- A utility thread that wakes up every second and flushes entries to the database
Inside the main thread, I sometimes need to do reads in the database. When this happens, performance is not important, but there is validity. (In an ideal world, I would read from the cache, not do it back and forth to the database, but put it aside for discussion.)
How can I make sure that the main thread sees the correct / resting database?
The standard mutex will not work, as I risk the main thread capturing the mutex before the data is dumped into the database. It will be a big race.
What I really want is some kind of mutex that allows the main thread to execute only after the mutex has been grabbed and released once. Is there such a thing? What is the best way to solve this problem?
UPDATE: after doing some additional research, I could use the Speed up conditional variable to solve this problem. Either that, or just bite a bullet and cache my notes. Thanks for the feedback!
c ++ multithreading mysql mutex
Runcible
source share