I'm developing an application with the following schema:
1 2 3 4 5 6 7 8
main thread:
- Creates another thread that periodically reads from DB and updates a shared object that is passed by reference.
- Starts a UNIX socket listening loop.
- Creates another thread when a new connection arrives. Let's call it working thread.
working thread:
- Receives by reference the same shared object that the DB thread receives.
- Reads and writes to the shared object.
The problem here is that I don't really know where add some MUTEX to prevent race conditions. Actually I use MUTEX in the working thread, always when I'm writing to the shared object. I think I must use the same MUTEX when updating the shared object in the DB thread. But, what happens when the working thread reads from the shared object? Must I use the MUTEX when reading?
Besides these questions, the application crashes sometimes with a SIGSEGV. Debugging with gdb I see that this happens, apparently randomly, when reading the object in the working thread...