【问题标题】:Thread Management Memory Leak线程管理内存泄漏
【发布时间】:2018-06-19 22:36:50
【问题描述】:

我在我的 C++ 应用程序中使用 reactor pattern。我使用线程向量std::vector <boost::thread> tvec {1000}; 和事件队列boost::lockfree::queue <int> events {1000}; 来处理事件。

我的事件调度器如下所示:

void my_class::event_dispatcher (void)
{
    INFO << "started";

    int event_element = 0;

    try
    {
        while (true)
        {
            // 1. wait for an event
            for (;;)
            {
                if (events.pop (event_element)) break;  // returns true if the queue is not empty
                boost::this_thread::sleep (boost::posix_time::milliseconds (250));      
            }

            // 2. handle event
            switch (event_element)
            {
                // 1-20 ...
                case 21:
                {
                    tvec.at(21).interrupt();
                    tvec.at(21) = boost::thread (boost::bind(&my_class::write_units_court_a, this));
                    break;
                }
                // 21-999...
                default:
                {
                    WARNING << "INVALID EVENT = " << event_element;
                    break;
                }
            }   
        }
    }
    catch (const std::exception &e)
    {
        ERROR << "e.what() = " << e.what();
        return;
    }

    INFO << "ended";

    return;
}

我的问题是,当我运行pmap -x PROGRAM_PID 时,它显示了许多这样的行:

00007f4063fd9000    8192      12      12 rw---   [ anon ]
00007f4063fd9000       0       0       0 rw---   [ anon ]
00007f40647d9000       4       0       0 -----   [ anon ]
00007f40647d9000       0       0       0 -----   [ anon ]
00007f40647da000    8192      16      16 rw---   [ anon ]
00007f40647da000       0       0       0 rw---   [ anon ]
00007f4064fda000       4       0       0 -----   [ anon ]
00007f4064fda000       0       0       0 -----   [ anon ]
00007f4064fdb000    8192      12      12 rw---   [ anon ]
00007f4064fdb000       0       0       0 rw---   [ anon ]
00007f40657db000       4       0       0 -----   [ anon ]
00007f40657db000       0       0       0 -----   [ anon ]

我认为这是我的事件调度程序在程序运行时创建和中断多个线程的内存泄漏。我在这些线程中使用 boost 日志记录并正确捕获线程中的中断;示例:

void my_class::write_units_court_a (void)
{
    INFO << "started";

    try
    {
        // working code here ...
    }
    catch (const std::exception &e)
    {
        ERROR << "e.what() = " << e.what();     
        return;
    }
    catch (boost::thread_interrupted)
    {
        INFO << "interrupted";
        return;
    }

    INFO << "ended";

    return;
}

是什么导致了这个看似线程内存泄漏的问题? Valgrind 显示内存丢失,我可以看到程序在启动和结束线程时使用了更多内存。

【问题讨论】:

  • 你能把它提炼成独立的东西吗?我不精通阅读pmap,所以也许你可以详细说明为什么你认为这些是泄漏
  • @sehe 我认为它们是由于this answer 而泄漏的。明天,我计划尝试boost::shared_ptr &lt;boost::thread&gt; thr 而不是线程向量,看看是否有帮助。我会回来报告的。

标签: c++ linux multithreading boost memory-leaks


【解决方案1】:

我现在使用boost::shared_ptr &lt;boost::thread&gt; thr_ev_21;

if (thr_ev_21.get()) thr_ev_21->interrupt();
thr_ev_21.reset(new boost::thread (boost::bind(&porter::write_units_court_a, this)));

现在我的内存使用量趋于平稳,而不是不断增长。线程对象不能在第二遍使用此行销毁:

tvec.at(21) = boost::thread (boost::bind(&my_class::write_units_court_a, this));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 2014-10-29
    • 2013-12-18
    • 2018-12-18
    • 2012-05-19
    • 2017-02-04
    • 2016-12-09
    相关资源
    最近更新 更多