I’ve got two threads working as producer, consumer. In the producer thread I’ve got the following code:
{
mediaQueue->PushEvent( boost::bind(/* params are not important */) );
return 0;
}
mediaQueue is a message queue and on a PushEvent() call a thread is notified that there is a job to be processed. The consumer thread just executes the functor, created with bind.
For me it’s really important that the producer thread returns BEFORE the consumer thread executes the functor.
So the question: Is it possible the producer to be interrupted just after it pushes the event but before it returns?
My research so far makes me think that it is possible and I should implement locking, but what’s your opinion on this?
The scheduler can interrupt your thread at any time. It doesn’t (necessarily) know or care what your thread is doing when its time is up. If there’s a possible race condition, yes, you must take the time and effort to implement correct locking.
In your case, it’s easy enough. Just wait on an object that’s set in the callee function on return from your
PushEventfunction.