首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >分离的线程在退出时崩溃

分离的线程在退出时崩溃
EN

Stack Overflow用户
提问于 2016-06-24 13:38:57
回答 3查看 1.5K关注 0票数 2

我使用了一个简单的线程池,如下所示-

代码语言:javascript
运行
复制
template<typename T>
class thread_safe_queue // thread safe worker queue.
{
private:
    std::atomic<bool> finish;
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    thread_safe_queue() : finish{ false }
    {}

    ~thread_safe_queue()
    {}

    void setDone()
    {
        finish.store(true);
        data_cond.notify_one();
    }
    void push(T new_value)
    {
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(std::move(new_value));
        data_cond.notify_one();
    }

    void wait_and_pop(T& value)
    {
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk, [this]
        {
            return false == data_queue.empty();
        });
        if (finish.load() == true)
            return;
        value = std::move(data_queue.front());
        data_queue.pop();
    }
    bool empty() const
    {
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
};

//Thread Pool
class ThreadPool
{
private:
    std::atomic<bool> done;
    unsigned thread_count;
    std::vector<std::thread> threads;

public:
    explicit ThreadPool(unsigned count = 1);

    ThreadPool(const ThreadPool & other) = delete;
    ThreadPool& operator = (const ThreadPool & other) = delete;

    ~ThreadPool()
    {
        done.store(true);
        work_queue.setDone();
        // IF thread is NOT marked detached and this is uncommented the worker threads waits infinitely.
        //for (auto &th : threads)
        //{
         //     if (th.joinable())
         //     th.join();
        // }
    }

    void init()
    {
        try
        {
            thread_count = std::min(thread_count, std::thread::hardware_concurrency());
            for (unsigned i = 0; i < thread_count; ++i)
            {
                threads.emplace_back(std::move(std::thread(&ThreadPool::workerThread, this)));
                threads.back().detach();
              // here the problem is if i dont mark it detatched thread infinitely waits for condition.
             // if i comment out the detach line and uncomment out comment lines in ~ThreadPool main threads waits infinitely.
            }
        }
        catch (...)
        {
            done.store(true);
            throw;
        }
    }

    void workerThread()
    {
        while (true)
        {
            std::function<void()> task;
            work_queue.wait_and_pop(task);
            if (done == true)
                break;
            task();
        }
    }
    void submit(std::function<void(void)> fn)
    {
        work_queue.push(fn);
    }
};

其用法如下:

代码语言:javascript
运行
复制
struct start
{
public:
    ThreadPool::ThreadPool m_NotifPool;
    ThreadPool::ThreadPool m_SnapPool;
    start()
    {
        m_NotifPool.init();
        m_SnapPool.init();
    }
};    
int main()
{
    start s;
    return 0;
}    

我在visual studio 2013上运行这段代码。问题是当主线程退出时。程序就会崩溃。它抛出异常。请帮帮我,我哪里做错了?如何正确停止工作线程?我已经花了相当多的时间,但仍在弄清楚问题所在。

提前感谢您的帮助。

EN

Stack Overflow用户

发布于 2016-06-24 15:12:51

main退出时,分离的线程被允许继续运行,但是,对象s被销毁。所以,当你的线程试图访问对象s的成员时,你遇到了UB。

有关您的问题的更多详细信息,请参阅此问题的接受答案:What happens to a detached thread when main() exits?

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38006274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档