以提升线程同步:: condition_variable [英] Thread synchronization with boost::condition_variable

查看:207
本文介绍了以提升线程同步:: condition_variable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的C ++多线程一些实验,我不知道如何解决一个问题。比方说,我们拥有的线程池,使用现有的线程和处理用户请求创建新的线程,当没有空闲线程可用。我创建command_queue线程安全类,其中有push和pop方法。流行而等待队列为空,只返回命令时,可用或发生超时。现在是时候来实现线程池。这样做是为了使空闲线程睡眠一定的时间,并杀死线程,如果没有什么一段时间后做。下面是实施

I'm doing some experiments on C++ multithreading and I have no idea how to solve one problem. Let's say we have thread pool, that process user requests using existing thread and creates new thread, when no free thread available. I've created command_queue thread-safe class, which have push and pop methods. pop waits while queue is empty and returns only when command is available or timeout occurred. Now it's time to implement thread pool. The idea is to make free threads sleep for some amount of time and kill the thread if there is nothing to do after that period of time. Here is implementation

command_queue::handler_t handler;
while (handler = tasks.pop(timeout))
{
    handler();
}

在这里,我们退出该线程过程是否发生超时。这是好的,但与新的线程创建的问题。比方说,我们已经有2线程处理用户请求,他们是做什么工作的,但我们需要以异步方式做一些其他的操作。
我们称之为

here we exit the thread procedure if timeout occurred. That is fine, but there is problem with new thread creation. Let's say we already have 2 thread processing user requests, they are working at the moment, but we need to do some other operation asynchronously. We call

thread_pool::start(some_operation);

这应该开始新的线程,没有可用的自由线程,因为。当线程可用它调用 TIMED_WAIT 的条件变量,这样的想法是要检查是否有线程在等待。

which should start new thread, because there is no free threads available. When thread is available it calls timed_wait on condition variable, so the idea is to check whether there are threads that are waiting.

if (thread_are_free_threads) // ???
   condition.notify_one();
else
   create_thread(thread_proc);

但如何检查呢?文档说,如果没有等待的线程notify_one什么都不做。如果我可以检查它是否什么也没做,这将是一个解决方案

but how to check it? Documentation says, that if there are no waiting threads notify_one does nothing. If I could check whether or not it did nothing that would be a solution

if (!condition.notify_one()) // nobody was notified
   create_thread(thread_proc);

据我看到没有办法检查。

As far as I see there is no way to check that.

谢谢您的回答。

推荐答案

您需要创建另一个变量(可能是一个信号),它知道有多少线程在运行,那么你可以检查,并创建一个新的线程,如果需要的话,前致电通知。

You need to create another variable (perhaps a semaphore) which knows how many threads are running, then you can check that and create a new thread, if needed, before calling notify.

另外,更好的选择是只没有你的线程退出时,他们超时。他们应该留下来通知活着的等待。相反,退出时的出通知时间,检查变量,看看如果程序仍在运行,或者如果它是关停,如果它仍在运行,然后再次开始等待。

The other, better option is to just not have your threads exit when they time out. They should stay alive waiting to be notified. Instead of exiting when the notify times out, check a variable to see if the program is still running or if it is "shutting down", If it's still running, then start waiting again.

这篇关于以提升线程同步:: condition_variable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆