I have the following piece of code to access a c++ queue (responseQueue) and send a response after doing some processing:
void sendRelpyToClient(){
if (responseQueue.empty())
return;
static int count = 0;
Result result;
SYNCHRONIZE() { //start of synchronization (pseudo-code)
result = responseQueue.front();
responseQueue.pop();
} //end of synchronization
if(count++ % 9 == 0)
{
//simulate some processing with a sleep
sleep for 15 seconds
}
result.sendResult();
}
The responseQueue contain instances of Result. The sendResult() method might take some processing time so that for some results that are stored in the queue, it might take relatively longer time for the method to return. The method sendRelpyToClient() is accessed by multiple threads, and if result.sendResult() is also in the SYNCHRONIZED() block, any other thread coming in can be blocked due to a sendResult() method which is taking a long time to return. This is the reason why I have chosen this approach.
My logic for this implementation is any thread accessing the responseQueue will first check is the queue is empty and return. since any thread accessing the SYNCHRONIZE() block will make the queue empty if there is only one item in it, there is no need to put this check inside the SYNCHRONIZE() block. If one thread who is accessing the queue is taken off (by the thread scheduler) right after the SYNCHRONIZE() block, the second thread again will take the next item in the queue and call result.sendResult(), and when the first thread resumes again it will continue to call result.sendResult() with the thread-local value of result (which that thread got from the queue before getiing blocked). The static count variable is there so that I can to simulate long processing for random threads using sleep, since as I’ve explained above sendResult() can take long time for some calls.
I ran a set of tests with this code, so far it has been doing ok. But I just wanted to ask the question here so that I can get your all’s ideas about this, if there is any issue with this approach. I’m not an expert on concurrent programming. And if there is a much better, efficient, clean ways to do it, maybe with concurrency not at the queue level but at individual data items’ level, please let me know those as well.
In general your approach is correct, but I want to note 2 things:
As was already proposed by Xeo, you should check if queue is empty within synchronized block.
You have static variable
countthat is global variable and you read/write it from several threads without synchronization. This is not that dangerous, but may lead to incorrect work of the algorithm. Remember that increment is not atomic operation. You might get situation when 2 threads simultaneously write a new value for count.UPD:
The fix is simple: