Here is what I essentially have:
I have thread A that periodically checks for messages and processes them.
Threads B and C need to send messages to A.
The problem arises when B and C or B or C try to send a message to A while A is processing a message and thus accessing the queue.
How is this problem usually solved?
Thanks
This is normally solved using mutexes, or other multi-thread protection mechanisms.
If you are working on windows, MFC provides the CMutex class for this problem.
If you are working on a posix system, the posix api provides the
pthread_mutex_lock,pthread_mutex_unlock, andpthread_mutex_trylockfunctions.Some basic pseudocode would be handy to demonstrate their use in your case:
For all three threads, their ability to act on the queue hinges on their ability to acquire the mutex – they will simply block and wait until the mutex is acquired. This prevents conflicts arising from the use of that resource.