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 5943683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:25:51+00:00 2026-05-22T16:25:51+00:00

This is more of a general design query I had. I have implemented a

  • 0

This is more of a general design query I had. I have implemented a publish / subscribe pattern by maintaining a list of subscribers. When an event to publish occurs, I loop through the subscribers and push the event to each one, of them in turn.

My problem occurs when due to that publication, somewhere in the depth of the software, another component or event the described component decide to unsubscribe themselves. By doing so, they invalidate my iterator and cause crashes.

What is the best way to solve this? I have been thinking of wrapping the whole publication loop into a try catch block, but that means some subscribers miss the particular subscription upon which someone unsubscribed, and seems a bit over the top. Then I tried feeding it back, e.g. I turned the void publish call into a bool publish call that returns true when the subscriber wants to be deleted, which works for that case, but not if another subscriber unsubscribes. Then I am thinking to “cache” unsubscription requests somewhere and release them when the loop is done, but that seems a bit overkill. Then I am thinking of storing the iterator as a class member, so that I can manipulate the iterator from outside, but that gets messy (say you unsubscribe subscriber 1, iterator is pointed at 2, and the container is a vector – then the iterator would have to be decremented). I think I might prefer one of the latter two solutions, but both seem not ideal.

Is this a common problem? Is there a more elegant solution?

  • 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-05-22T16:25:51+00:00Added an answer on May 22, 2026 at 4:25 pm

    You could either disallow subscription operations during publication, or you could use an appropriate data structure to hold your subscription list, or both.

    Assuming that you keep your subscribers in a std::list, you could run your loop thus:

    for(iterator_type it = subs.begin(); it != subs.end(); ) {
        iterator_type next = it;
        ++next;
        it->notifier();
        it = next;
    }
    

    That way, if the current item is removed, you still have a valid iterator in next. Of course, you still can’t allow arbitrary removal (what if next is removed?) during publication.

    To allow arbitrary removal, mark an item as invalid and defer its list removal until it is safe to do so:

    ... publication loop ...
    dontRemoveItems = true;
    for(iterator_type it = subs.begin(); it != subs.end(); ++it) {
        if(it->valid)
            it->notifier();
    }
    std::erase(std::remove_if(...,, IsNotValid),...);
    dontRemoveItems = false;
    

    elsewhere,

    ... removal code:
    if(dontRemoveItems) item->valid = false;
    else subs.erase(item);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a general design question I guess... I have an Ajax
This is more of a general design question, but it will be implemented in
This is more of a general design query with Core Data and how to
This is more of a design question that others may have had similar experience
I suppose this question is more general than working with COM components. I have
I have a Yesod application (but question is more general than this) that allows
I asked this question in a more general design context before. Now, I'd like
I have this general problem in design, refactoring or triage: I have an existing
Although I'm programming in C++, this is more of a general question about design.
This is more a general question but my particular case involves a ruby/rails app

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.