简体   繁体   English

如何使用boost :: thread创建一个threadpool

[英]how to make a threadpool with boost::thread

boost::thread is not-a-thread , a new thread is created when the ftor passed to it is called and thread exits when ftor returns. boost::thread 不是一个线程 ,当ftor传递给它的ftor时创建一个new thread ,当ftor返回时线程退出。

We use threadpool to minimize thread creation and destruction cost. 我们使用threadpool来最小化线程创建和销毁成本。 but each thread in threadpool is also destroyed when the supplied ftor returns. 但是当提供的ftor返回时,线程池中的每个线程也会被销毁。

So whats the basic concept behind building a threadpool ? 那么构建线程池背后的基本概念是什么? is there any permanent thread where I can assign ftors to that thread ? 有没有永久线程,我可以将ftors分配给该线程?

A thread pool is just a bunch of threads that already running, and that are all running the same function. 线程池只是已经运行的一堆线程,并且都运行相同的功能。 This functions basically just waits on a queue, and when there is a "function" in the queue it extracts and executes it. 这个函数基本上只是等待队列,当队列中有一个“函数”时,它会提取并执行它。

Pseudo-code: 伪代码:

void thread_pool_function()
{
    while (true)
    {
        wait_for_signal_that_queue_is_not_empty();

        function_to_call = queue.remove_top();

        unklock_queue_semaphore();

        function_to_call();
    }
}

create_thread(thread_pool_function);
create_thread(thread_pool_function);
create_thread(thread_pool_function);
create_thread(thread_pool_function);

In the "code" above there are now four threads, all initially waiting for something to be put in a "queue". 在上面的“代码”中,现在有四个线程,最初都在等待将某些东西放入“队列”中。 When there is something in the queue, it extracts it, and calls it as a function. 当队列中有某些内容时,它会将其解压缩并将其作为函数调用。

This is probably the simplest way to implement a thread pool. 这可能是实现线程池的最简单方法。

In addtion to what @Joachim posted: 除了@Joachim发布的内容之外:

One way to flow-control such a system (and one I use a lot), is to use a 'pool queue', (blocking producer-consumer queue), of tasks, created and filled at startup with a fixed number of task objects. 流控制这样一个系统(以及我经常使用的一个)的一种方法是使用“池队列”(阻塞生产者 - 消费者队列),在启动时使用固定数量的任务对象创建和填充的任务。 Any thread that wants to issue a task has to get one from the pool first and tasks are returned to the pool after completion handling. 任何想要发出任务的线程都必须先从池中获取一个,并在完成处理后将任务返回到池中。 This limits the number of tasks in the system and, if the pool empties, requesting threads just have to wait, blocked on the empty pool, until some 'used' tasks come back in. 这限制了系统中的任务数量,如果池清空,请求线程只需等待,在空池上阻塞,直到某些“已使用”任务重新进入。

This works well, provides flow-control, prevents memory-runaway and eliminates continual task create/destroy. 这很好用,提供流量控制,防止内存失控,并消除连续任务创建/销毁。 It's also easy to periodically display/write the pool queue depth on a timer, so you can see how 'busy' your app is, (and detect any leaks:). 在计时器上定期显示/写入池队列深度也很容易,因此您可以看到应用程序的“繁忙”程度,(并检测任何泄漏:)。

Edit: Also, it removes the need for any bounded queues in the system. 编辑:此外,它消除了系统中任何有界队列的需要。 Unbounded queues are simpler and tend to need fewer system calls. 无界队列更简单,往往需要更少的系统调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM