Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:40:28+00:00 2026-06-11T16:40:28+00:00

I have the following piece of code to access a c++ queue (responseQueue) and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:40:29+00:00Added an answer on June 11, 2026 at 4:40 pm

    In general your approach is correct, but I want to note 2 things:

    1. As was already proposed by Xeo, you should check if queue is empty within synchronized block.

    2. You have static variable count that 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:

        void sendRelpyToClient(){    
            static int count = 0;
            Result result;
    
            bool execute = false;
    
            SYNCHRONIZE() { //start of synchronization (pseudo-code)
                if (responseQueue.empty())
                   return;
               result = responseQueue.front();
               responseQueue.pop();
    
               execute = (count++ % 9 == 0);
            } //end of synchronization
    
            if (execute)
            {
                //simulate some processing with a sleep
                sleep for 15 seconds
            }
            result.sendResult();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code that takes in some words, stores them
I have the following piece of code: class Student { public: Student(){} void display()
I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var
I have the following piece of code to parse a csv file. After that
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code in my Model: public function getSite() {
I have the following piece of code where i am concatenating events to the
I have the following piece of code and in the j for loop the
I have the following piece of code which outputs a long article: <?php the_content();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.