【发布时间】:2023-03-07 23:16:01
【问题描述】:
我正在根据 boost 网站上的示例构建应用程序。以下是需要了解的相关定义:
typedef boost::shared_ptr连接_ptr; std::setconnection 是一个类。
在其中一个文件connection_manager.cpp 中,他们这样做:
无效 connection_manager::start(connection_ptr c) { 连接_.插入(c); c->开始(); }现在,由于我的程序结构,我想在单独的线程中启动每个新连接。所以我根据上面的修改:
无效 connection_manager::start(connection_ptr c) { boost::shared_ptr< boost::thread > thread(新的 boost::thread( boost::bind(&connection::start, c))); // 将新创建的线程推入向量中 线程.push_back(线程); // 将连接保存在我们的集合中 连接_.插入(c); }我的问题,因此这个问题,是当我只想这些 connection_ 对象之一。在前面提供的链接中,他们这样做:
无效 connection_manager::stop(connection_ptr c) { 连接_.erase(c); c->停止(); // 找到线程之间的连接并加入该线程 }但正如上面的评论所暗示的,我如何在所有线程中找到 c 并仅停止该线程。我想为该线程调用 join() 函数。
更新:
我认为这实际上是我真正想要的!所以我将我的变量声明为
std::map < connection_ptr, boost::shared_ptr < boost::thread > > 线程;但是,如何像以前一样创建新胎面?喜欢:
boost::shared_ptr < boost::thread > thread(新的 boost::thread( boost::bind(&connection::start, c)));但是下一步是什么?很抱歉让您感到困惑... :-)
【问题讨论】:
标签: c++ boost threadpool boost-thread boost-bind