从C ++ 11中的线程返回值 [英] Return a value from a thread in C++11

查看:67
本文介绍了从C ++ 11中的线程返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++ 11中的线程返回值的最有效方式是什么?

What's the most efficient way to return a value from a thread in C++11?

vector<thread> t(6);

for(int i = 0; i < 6; i++)
    t[i] = thread(do_c);

for(thread& t_now : t)
    t_now.join();

for(int i = 0; i < 6; i++)
    cout << /*the return of function do_c*/

此外,如果所做的更改将对性能有所帮助,请随时推荐除 std :: thread 之外的其他线程.

Also, if the change will benefit performance, feel free to recommend another thread than std::thread.

推荐答案

启动线程并终止线程需要数百个机器周期.但这仅仅是个开始.如果线程在做任何有用的事情,线程之间的上下文切换势必会发生,这将反复消耗甚至更多的数百个机器周期.所有这些线程的执行上下文将占用很多字节的内存,从而又将占用很多缓存行,从而阻碍了CPU大量数百个机器周期的工作.

Lauching a thread and terminating it require many hundreds of machine cycles. But that's only a beginning. Context switches between threads, that are bound to happen if the threads are doing anything useful, will repeatedly consume even more many hundreds of machine cycles. The execution context of all these threads will consume many a byte of memory, which in turn will mess up many a line of cache, thus hindering the CPU efforts for yet another great deal of many hundreds of machine cycles.

事实上,多任务处理是数百个机器周期的重要消耗者.当您设法使足够多的处理器处理概念上独立的数据块时,多任务处理仅在 CPU功耗 方面才是有利可图的(因此并行处理不会威胁其完整性)),并且与单处理器版本相比足以显示出净收益.

As a matter of fact, doing anything with multitasking is a great consumer of many hundreds of machine cycles. Multitasking only becomes profitable in terms of CPU power usage when you manage to get enough processors working on lumps of data that are conceptually independent (so parallel processing won't threaten their integrity) and big enough to show a net gain compared with a monoprocessor version.

在所有其他情况下,多任务处理在所有领域中本质上都是效率低下的,只有一种: 反应性 .一项任务可以对外部事件做出非常快速而准确的反应,而该事件最终来自某个外部硬件组件(无论是计时器的内部时钟还是网络流量的WiFi/以太网控制器).

In all other cases, multitasking is inherently inefficient in all domains but one: reactivity. A task can react very quickly and precisely to an external event, that ultimately comes from some external H/W component (be it the internal clock for timers or your WiFi/Ethernet controller for network traffic).

这种在不浪费CPU的情况下等待外部事件的能力提高了总体CPU效率.就是这样.
就其他性能参数(内存消耗,内核调用内浪费的时间等)而言,启动新线程总是净损失 .

This ability to wait for external events without wasting CPU is what increases the overall CPU efficiency. And that's it.
In terms of other performance parameters (memory consumption, time wasted inside kernel calls, etc), launching a new thread is always a net loss.

简而言之,多任务编程的艺术可以归结为:

In a nutshell, the art of multitasking programming boils down to:

  • 确定您将要处理的外部I/O流
  • 考虑到反应性要求(记住更多的反应性= 99%的时间CPU/内存效率降低)
  • 为所需事件设置处理程序,以合理的效率/轻松地进行维护.

多处理器体系结构正在将复杂性提高到一个新的水平,因为现在任何程序都可以看作是一个手头有多个外部CPU的进程,可以将其用作附加电源.但是您的问题似乎与此无关.

Multiprocessor architectures are adding a new level of complexity, since any program can now be seen as a process having a number of external CPUs at hand, that could be used as additional power sources. But your problem does not seem to have anything to do with that.

多任务处理效率的衡量最终取决于外部事件的数量,该给定程序应同时并在给定的反应性限制范围内进行处理.

A measure of multitasking efficiency will ultimately depend on the number of external events a given program is expected to cope with simultaneously and within a given set of reactivity limits.

最后我要问您一个具体问题.

At last I come to your particular question.

要对外部事件做出反应,每次必须在蚁穴周围移动新的树枝或死虫时启动任务,这是一种非常粗糙且效率低下的方法.

To react to external events, launching a task each time a new twig or bit of dead insect has to be moved around the anthill is a very coarse and inefficient approach.

您可以使用许多强大的同步工具,这些工具可以使您在单个任务上下文中以(几乎)最佳效率(几乎)无成本地对一堆异步事件做出反应.
通常,阻塞会等待多个输入,例如,带有unix风格的 select()或Microsoft的 WaitForMultipleEvents()对应对象.

You have many powerful synchronization tools at your disposal, that will allow you to react to a bunch of asynchronous events from within a single task context with (near) optimal efficiency at (virtually) no cost.
Typically, blocking waits on multiple inputs, like for instance the unix-flavoured select() or Microsoft's WaitForMultipleEvents() counterpart.

使用这些功能将使您获得的性能提升比从您的任务结果收集优化项目中挤出的几十个CPU周期要大得多.

Using these will give you a performance boost incomparably greater than the few dozen CPU cycles you could squeeze out of this task-result-gathering-optimization project of yours.

所以我的答案是:根本不用理会优化线程设置.这不是问题.

So my answer is: don't bother with optimizing thread setup at all. It's a non-issue.

您的时间最好花在重新思考您的体系结构上,这样一来,经过深思熟虑的线程就可以取代当前设计产生的大量无用的CPU和内存消耗.

Your time would be better spent rethinking your architecture so that a handful of well thought out threads could replace the hordes of useless CPU and memory hogs your current design would spawn.

这篇关于从C ++ 11中的线程返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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