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

  • SEARCH
  • Home
  • 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 8521385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:48:34+00:00 2026-06-11T06:48:34+00:00

I am receiving updates from two different sources (network listen) upon which I get

  • 0

I am receiving updates from two different sources (network listen) upon which I get a callback to a method somewhat like this:

void onUpdate(Update* update)
{
    static MutexT lock;
    static hash_set<UpdateID> set;

    ScopedGuard _(lock); //lock here
    hash_set<UpdateID>::iterator it = set.find(update->id);
    if (it == set.end())
        set.insert(update->id);
    else
        return;

    listener->onUpdate(/*some stuff*/);
}

since both sources are feeding you the same updates, you want to avoid notifying on duplicates, you want to arbitrate between both sources to be up to date with the latest from whoever gives it to you first and also for missed updates if one sources is possibly unreliable. The thing is, it’s expensive to lock on every update, is there a way around this lock if I definitely do not want duplicate onUpdate calls?

(Or at least a way to reduce cost?)

  • 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-11T06:48:36+00:00Added an answer on June 11, 2026 at 6:48 am

    First of all, the lock shouldn’t be static, it should be a member variable.

    You could make it more efficient by using a reader/writer mutex, e.g.

    boost::shared_mutex          mutex_;
    std::unordered_set<UpdateID> set_;
    
    void onUpdate(const Update& update)
    {
        boost::upgrade_lock<boost::shared_mutex> lock(mutex_);
    
        auto it = set_.find(update.id);
        if(it == set_.end())
        {
            boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(mutex_);
            set.insert(update.id);
        }
    
        listener->onUpdate(/*some stuff*/);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Via $_GET method, I am receiving two hashtag variables (football teams) from a drop
I'm basically trying to get all touch event data from something like a system
I am receiving a parse error which is this: Parse error: syntax error, unexpected
I am receiving an xml feed which has values such as: <Theme>Valentine&#39;s Day</Theme> <Copyright>&#169;
In MySQL, is it possible to select two values from a column (with limit
From my understanding you cannot socket a connection between two iPhones (correct me if
When receiving user input on forms I want to detect whether fields like username
Lately I have been receiving informations from users about my alarm app not ringing
I am receiving a stream of data from a webservice and trying to save
I've made a simple JS which retrives data from my servlet. Data which I'm

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.