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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:36:35+00:00 2026-05-22T01:36:35+00:00

I am building an application based on an example on the boost website. These

  • 0

I am building an application based on an example on the boost website. These are the relevant definitions to know of:

typedef boost::shared_ptr< connection > connection_ptr;
std::set< connection_ptr> connections_;
std::vector< boost::shared_ptr< boost::thread> > threads;

where connection is a class.

In one of the files, connection_manager.cpp, they are doing like this:

void connection_manager::start(connection_ptr c)
{
  connections_.insert(c);
  c->start();
}

Now, I want to start every new connection in a separate thread due to my program structure. So I have modified the above according to:

void connection_manager::start(connection_ptr c)
{
    boost::shared_ptr< boost::thread > thread(new boost::thread(
                        boost::bind(&connection::start, c)));

    // push the newely created thread into the vector
    threads.push_back(thread);

    // save the connection in our set
    connections_.insert(c);
}

My problem, and hence this question, is when I want to only one of these connection_ objects. In the link provided earlier, they do like this:

void connection_manager::stop(connection_ptr c)
{
    connections_.erase(c);
    c->stop();

    // find the connection in among the threads and join that thread
}

But as the comment above suggests, how do I find c amongst all the threads and stop only that thread. I want to call the join() function for that thread.


Update:

I think that this is actually what I really want to have! So I declare my variable as

std::map < connection_ptr, boost::shared_ptr < boost::thread > > threads;

But then, how do I create a new tread, same way as before? Like:

boost::shared_ptr < boost::thread > thread(new boost::thread(
                        boost::bind(&connection::start, c)));

But what is then the next step? Sorry for being confused… 🙂

  • 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-22T01:36:36+00:00Added an answer on May 22, 2026 at 1:36 am

    If you use a map (or other associative container) instead of vector you can maintain an association between connections and threads:

    std::map<connection_ptr, boost::shared_ptr<boost::thread> > threads;
    

    allows you to write:

    threads[connection] = thread;
    

    instead of the push_back call after you create the thread and then:

    threads[connection]->stop();
    

    later on when you want to look it up.

    NB: As a general comment applications which maintain a 1:1 mapping between threads and network connections are quite vulnerable to Denial of Service (DoS) attacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an application that is based on MVVM-Light. I am in the
I'm building an Android application that's based around an enhanced WebView (based on PhoneGap).
I'm building a navigation controller based iPhone application and am curious how to go
I am building application that required some data from iPhone's Call log(read only). The
When building an application, is there any meaningful difference between the idea of Find
I'm building an application that is targeting Windows, Mac and Linux soon. I was
I'm building an application in C# using WPF. How can I bind to some
We're currently building an application that executes a number of external tools. We often
I am building an application where a page will load user controls (x.ascx) dynamically
I am building an application where I want to be able to click a

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.