Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadPool.hpp中RunInThread函数的一点疑问。 #35

Open
hizoubin opened this issue Sep 20, 2020 · 2 comments
Open

ThreadPool.hpp中RunInThread函数的一点疑问。 #35

hizoubin opened this issue Sep 20, 2020 · 2 comments

Comments

@hizoubin
Copy link

hizoubin commented Sep 20, 2020

你好! RunInThread中一次把同步队列中的任务都右值引用了,这样也是单个线程里依次在跑所有的任务啊。而不是分别在各个线程中跑一个任务?
请问我的理解有错误么?谢谢

    void RunInThread()
    {
        while (m_running)
        {
            //取任务分别执行
            std::list<Task> list;
            m_queue.Take(list);

            for (auto& task : list)
            {
                if (!m_running)
                    return;

                task();
            }
        }
    }
@qicosmos
Copy link
Owner

这个看使用场景,如果生产者和消费者达到一个平衡的情况下,比如每次入队几个任务,每次出队几个任务的时候,一个线程一次取走全部的几个任务是可以的,还能有一点性能优化,不必每次取任务都去加锁一次,这里是取一批任务加锁一次。一般情况下不这样用,还是一次一个任务的好。

@valaxkong
Copy link

@qicosmos 大佬解释下为什么一次一个任务好?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants