C++ 为什么这个线程管理模式会导致死锁?

C++ 为什么这个线程管理模式会导致死锁?,c++,deadlock,boost-thread,C++,Deadlock,Boost Thread,我正在使用一个公共基类has\u threads来管理应该允许实例化boost::thread的任何类型 的实例有_线程每个线程都有一个线程集(支持waitAll和interruptAll函数,我在下面不包括这些函数),当线程终止时应自动调用removeThread,以保持该线程集的完整性 在我的程序中,我只有一个。每隔10秒创建一次线程,每个线程执行一次数据库查找。当查找完成时,线程运行到completion,应该调用removeThread;设置互斥锁后,线程对象将从内部跟踪中删除。我可以看

我正在使用一个公共基类
has\u threads
来管理应该允许实例化
boost::thread
的任何类型

的实例有_线程
每个线程都有一个
线程集
(支持
waitAll
interruptAll
函数,我在下面不包括这些函数),当线程终止时应自动调用
removeThread
,以保持该
线程集的完整性

在我的程序中,我只有一个。每隔10秒创建一次线程,每个线程执行一次数据库查找。当查找完成时,线程运行到completion,应该调用
removeThread
;设置互斥锁后,线程对象将从内部跟踪中删除。我可以看到它在输出
ABC
时正常工作

不过,偶尔,这些机制会发生碰撞
removeThread
可能同时执行两次。我不明白的是为什么这会导致僵局。从这一点开始的所有线程调用都不会输出除
A
之外的任何内容。[值得注意的是,我使用的是线程安全的stdlib,当不使用IOStreams时,问题仍然存在。]堆栈跟踪表明互斥锁正在锁定这些线程,但是为什么第一个线程不最终释放第二个线程的锁,第二个线程不释放第三个线程的锁,依此类推

我是否遗漏了有关
作用域锁定
工作原理的一些基本信息?尽管使用了互斥锁(甚至是由于?),这里有没有任何明显的、我没有注意到的、可能导致死锁的东西

很抱歉这个糟糕的问题,但我相信您已经意识到,为这样的bug提供真正的测试用例几乎是不可能的

class has_threads {
    protected:
        template <typename Callable>
        void createThread(Callable f, bool allowSignals)
        {
            boost::mutex::scoped_lock l(threads_lock);

            // Create and run thread
            boost::shared_ptr<boost::thread> t(new boost::thread());

            // Track thread
            threads.insert(t);

            // Run thread (do this after inserting the thread for tracking so that we're ready for the on-exit handler)
            *t = boost::thread(&has_threads::runThread<Callable>, this, f, allowSignals);
        }

    private:

        /**
         * Entrypoint function for a thread.
         * Sets up the on-end handler then invokes the user-provided worker function.
         */
        template <typename Callable>
        void runThread(Callable f, bool allowSignals)
        {
            boost::this_thread::at_thread_exit(
                boost::bind(
                    &has_threads::releaseThread,
                    this,
                    boost::this_thread::get_id()
                )
            );

            if (!allowSignals)
                blockSignalsInThisThread();


            try {
                f();
            }
            catch (boost::thread_interrupted& e) {

                // Yes, we should catch this exception!
                // Letting it bubble over is _potentially_ dangerous:
                // http://stackoverflow.com/questions/6375121

                std::cout << "Thread " << boost::this_thread::get_id() << " interrupted (and ended)." << std::endl;
            }
            catch (std::exception& e) {
                std::cout << "Exception caught from thread " << boost::this_thread::get_id() << ": " << e.what() << std::endl;
            }
            catch (...) {
                std::cout << "Unknown exception caught from thread " << boost::this_thread::get_id() << std::endl;
            }
        }

        void has_threads::releaseThread(boost::thread::id thread_id)
        {
            std::cout << "A";
            boost::mutex::scoped_lock l(threads_lock);

            std::cout << "B";
            for (threads_t::iterator it = threads.begin(), end = threads.end(); it != end; ++it) {

                if ((*it)->get_id() != thread_id)
                    continue;

                threads.erase(it);
                break;
            }
            std::cout << "C";
        }

        void blockSignalsInThisThread()
        {
            sigset_t signal_set;
            sigemptyset(&signal_set);
            sigaddset(&signal_set, SIGINT);
            sigaddset(&signal_set, SIGTERM);
            sigaddset(&signal_set, SIGHUP);
            sigaddset(&signal_set, SIGPIPE); // http://www.unixguide.net/network/socketfaq/2.19.shtml
            pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
        }


        typedef std::set<boost::shared_ptr<boost::thread> > threads_t;
        threads_t threads;

        boost::mutex threads_lock;
};

struct some_component : has_threads {
    some_component() {
        // set a scheduler to invoke createThread(bind(&some_work, this)) every 10s
    }

    void some_work() {
        // usually pretty quick, but I guess sometimes it could take >= 10s
    }
};
类有\u个线程{
受保护的:
模板
void createThread(可调用f,bool allowSignals)
{
boost::mutex::作用域锁定l(线程锁定);
//创建并运行线程
boost::shared_ptr t(新的boost::thread());
//跟踪线程
螺纹.插入件(t);
//运行线程(在插入用于跟踪的线程后执行此操作,以便我们准备好退出处理程序)
*t=boost::thread(&has_threads::runThread,this,f,allowSignals);
}
私人:
/**
*线程的入口点函数。
*设置终端处理程序,然后调用用户提供的辅助函数。
*/
模板
void runThread(可调用f,bool allowSignals)
{
boost::this_thread::at_thread_exit(
boost::bind(
&has_threads::releaseThread,
这
boost::this_线程::get_id()
)
);
如果(!allowSignals)
blockSignalsInThisThread();
试一试{
f();
}
捕获(boost::线程\u和e){
//是的,我们应该抓住这个例外!
//让它冒泡是潜在的危险:
// http://stackoverflow.com/questions/6375121

std::cout如果同一线程锁定了它已经锁定的互斥锁(除非使用递归互斥锁),则可能会发生死锁

如果发布部分第二次被同一个线程调用,就像代码中发生的一样,那么就会出现死锁

我没有详细研究您的代码,但您可能必须重新设计代码(simplify?),以确保同一线程不能两次获取锁。您可能可以使用保护检查锁的所有权

编辑: 正如我在评论和IronMensan回答中所说的,一种可能的情况是线程在创建期间停止,在释放代码创建部分锁定的互斥锁之前调用at_出口

编辑2:

对于互斥锁和作用域锁,我只能想象一个递归锁,或者一个未释放的锁。例如,如果一个循环由于内存损坏而变为无穷大,就会发生这种情况

我建议添加更多带有线程id的日志,以检查是否存在递归锁或奇怪的东西。然后我将检查我的循环是否正确。我还将检查每个线程只调用一次at_出口

还有一件事,检查在at_exit函数中擦除线程(从而调用析构函数)的效果


我的2美分

您可能需要这样做:

    void createThread(Callable f, bool allowSignals) 
    { 
        // Create and run thread 
        boost::shared_ptr<boost::thread> t(new boost::thread()); 

        {
            boost::mutex::scoped_lock l(threads_lock); 

            // Track thread 
            threads.insert(t);
        } 

        //Do not hold threads_lock while starting the new thread in case
        //it completes immediately

        // Run thread (do this after inserting the thread for tracking so that we're ready for the on-exit handler) 
        *t = boost::thread(&has_threads::runThread<Callable>, this, f, allowSignals); 
    } 
void createThread(可调用f,bool allowSignals)
{ 
//创建并运行线程
boost::shared_ptr t(新的boost::thread());
{
boost::mutex::作用域锁定l(线程锁定);
//跟踪线程
螺纹.插入件(t);
} 
//在启动新线程时不要保持线程锁定,以防
//它马上就完成了
//运行线程(在插入用于跟踪的线程后执行此操作,以便我们准备好退出处理程序)
*t=boost::thread(&has_threads::runThread,this,f,allowSignals);
} 
换句话说,专门使用
线程锁定
来保护
线程

更新:

为了进一步说明注释中关于boost::thread如何工作的推测,锁模式可以如下所示:

    void createThread(Callable f, bool allowSignals) 
    { 
        // Create and run thread 
        boost::shared_ptr<boost::thread> t(new boost::thread()); 

        {
            boost::mutex::scoped_lock l(threads_lock); 

            // Track thread 
            threads.insert(t);
        } 

        //Do not hold threads_lock while starting the new thread in case
        //it completes immediately

        // Run thread (do this after inserting the thread for tracking so that we're ready for the on-exit handler) 
        *t = boost::thread(&has_threads::runThread<Callable>, this, f, allowSignals); 
    } 
createThread

  • createThread
    )获取
    threads\u锁
  • boost::thread::opeator=
    )获取
    boost::thread
    内部锁
  • boost::thread::opeator=
    )释放
    boost::thread
    内部锁
  • createThread
    )释放
    threads\u锁
  • 线程结束处理程序:

  • at_thread_exit
    )获取
    boost::thread
    内部锁
  • releaseT