问题描述
这是松散的与此相关的问题:这个例子说明好听:
{
std::async(std::launch::async, []{ f(); });
std::async(std::launch::async, []{ g(); }); // does not run until f() completes
}
另一个澄清:
Another clarification:
我知道,线程池可以有其他用途legimate,但这个问题我只在有趣昂贵避免线程创建成本方面
我觉得仍有一些线程池是非常有用的,特别是如果你需要对资源更多的控制情况。
例如,一台服务器可能会决定simulatenously处理只请求的一个固定数,以保证快速的反应时间和增加内存使用的predictability。线程池应该没事,在这里。
I think there are still situations where thread pools are very useful, especially if you need more control over resources.For example, a server might decide to handle only a fixed number of requests simulatenously to guarantee fast response times and to increase the predictability of memory usage. Thread pools should be fine, here.
线程局部变量也可能是你自己的线程池的说法,但我不知道它是否是revelant在实践中:
Thread-local variables may also be an argument for your own thread pools, but I'm not sure whether it is revelant in practice:
- 创建具有
一个新的线程的std ::线程
启动时没有初始化的线程局部变量。也许这是不是你想要的。 - 在由
异步
产生的线程,是因为线程可能被重复使用对我来说是有些不清楚。从我的理解,线程局部变量,不能保证被重置了,但我可能是错了。 - 使用您自己的(固定大小)线程池,在另一方面,为您提供了完全控制,如果你真的需要它。
- Creating a new thread with
std::thread
starts without initialized thread-local variables. Maybe this is not what you want. - In threads spawned by
async
, it is somewhat unclear for me because the thread could have been reused. From my understanding, thread-local variables are not guaranteed to be resetted, but I may be mistaken. - Using your own (fixed-size) thread pools, on the other hand, gives you full control if you really need it.
推荐答案
问题1
我知道在Linux中创建线程已经取得了非常便宜。所以没有太多的理由,线程池,以避免在Linux中创建线程的成本。
I know that in Linux thread creation has been made very cheap. And so there isn't much of a reason to have thread pools to avoid thread creation costs in Linux.
我也并不认为标准库使得线程的创建,而透明的真正影响这个计算了。它只是使它看起来''更快,更容易启动一个线程。这取决于比C ++更您的操作系统。恕我直言,操作系统有很多,他们应该做的线程创建尽可能便宜的原因。循环自动并行化就是一个例子。
I also don't think the standard library making thread creation rather transparent really affects this calculation much. It just makes it 'seem' faster and easier to launch a thread. It depends more on your OS than on C++. IMHO, OSes have a lot of reasons they should be making thread creation as cheap as possible. Auto-parallelization of loops is just one example.
问题2
是的,基本上这种含蓄启动一个线程。但实际上,它仍然是相当明显发生了什么。所以,我真的不认为这个词隐含一个特别好的词。
Yes, basically this 'implicitly' launches a thread. But really, it's still quite obvious what's happening. So I don't really think the word implicitly is a particularly good word.
问3
我个人喜欢线程启动要明确。我把岛屿上,你能保证按顺序访问了很多价值。否则,你最终与可变状态,你总是要围绕地方包裹一个互斥体,并记住使用它。
Personally, I like thread launches to be explicit. I place a lot of value on islands where you can guarantee serial access. Otherwise you end up with mutable state that you always have to be wrapping a mutex around somewhere and remembering to use it.
我喜欢的工作队列模型,因为有躺在附近,因此您可以更有效地处理可变状态的串行孤岛比未来模型好多了。
I liked the work queue model a whole lot better than the 'future' model because there are 'islands of serial' lying around so you can more effectively handle mutable state.
不过说真的,这取决于你在做什么。
But really, it depends on exactly what you're doing.
这篇关于将异步(发射::异步)在C ++ 11化妆线程池过时,避免昂贵的创建线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!