std::launch::async is blocking like a sync process

I am running Visual Studio 2012 and attempting to learn how std::async works. I have created a very simple C++ console application: #include "stdafx.h" #include <future> #include <iostream> void foo() { std::cout << "foo() thread sleep" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(5)); std::cout << "foo() thread awake" << std::e

std :: launch :: async像同步过程一样阻塞

我正在运行Visual Studio 2012并试图了解std :: async如何工作。 我创建了一个非常简单的C ++控制台应用程序: #include "stdafx.h" #include <future> #include <iostream> void foo() { std::cout << "foo() thread sleep" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(5)); std::cout << "foo() thread awake" << std::endl; } int main() { std

std::async with temporary future returned

Here is exerpt from en.cppreference.com If the std::future obtained from std::async has temporary object lifetime (not moved or bound to a variable), the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes, essentially code such as the following synchronous: std::async(std::launch::async, []{ f(); }); // temporary's dtor waits f

std :: async临时将来返回

这里是从en.cppreference.com exerpt 如果从std :: async获得的std :: future具有临时对象生存期(未移动或绑定到变量),则std :: future的析构函数将在完整表达式的末尾处阻塞,直到异步操作完成代码如下面的同步: std::async(std::launch::async, []{ f(); }); // temporary's dtor waits for f() std::async(std::launch::async, []{ g(); }); // does not start until f() completes 但是当我用VC ++尝试时: void fas

Weird behavior with exceptions thrown from std::async, in MSVC 2015

I just upgrade from Visual Studio 2013 to 2015, and I'm running into a bunch of issues with things that used to work in 2013, but which do not in 2015. For example, here's one that has me stumped. I created a test-case out of the original code. Basically, the code runs some operations in a thread, via std::async(). Within the thread, exceptions might be thrown (A), which should be p

奇怪的行为,MSVC 2015中从std :: async引发异常

我只是从Visual Studio 2013升级到2015,我遇到了一些问题,这些问题曾经在2013年开始运作,但在2015年不会。 例如,这是一个让我难倒的人。 我从原始代码中创建了一个测试用例。 基本上,代码通过std :: async()在一个线程中运行一些操作。 在线程内,可能会抛出异常(A),它应放置在未来由std :: async()返回的对象中。 奇怪的是,在(B)中,Ex的析构函数被调用,但是对象仍然被抛出。 在try-block中,当ex(D)

Why should I use std::async?

I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 : it's called async, but it got a really "sequential behaviour", basically in the row where you call the future associated with your async function foo, the program blocks until the execution of foo

为什么我应该使用std :: async?

我试图深入探索新的C ++ 11标准的所有选项,同时使用std :: async并阅读其定义,我注意到了两件事情,至少在linux下使用gcc 4.8.1: 它被称为异步,但它有一个真正的“顺序行为”,基本上在你调用将与异步函数foo关联的未来的行中,程序将阻塞,直到foo执行完成。 它依赖于与其他外部库完全相同的外部库,以及更好的非阻塞解决方案,这意味着pthread ,如果要使用std::async ,则需要pthread。 在这一点上,我很自然地问为什

Are the days of passing const std::string & as a parameter over?

I heard a recent talk by Herb Sutter who suggested that the reasons to pass std::vector and std::string by const & are largely gone. He suggested that writing a function such as the following is now preferable: std::string do_something ( std::string inval ) { std::string return_val; // ... do stuff ... return return_val; } I understand that the return_val will be an rvalue at the

传递const std :: string&作为参数的日子已经过去了吗?

我听到Herb Sutter最近的一次谈话,他建议通过const & std::vector和std::string在很大程度上没有了。 他建议编写一个如下的函数现在更可取: std::string do_something ( std::string inval ) { std::string return_val; // ... do stuff ... return return_val; } 我明白return_val在函数返回时是一个右值,因此可以使用移动语义来返回,这很便宜。 但是, inval仍然大于参考的大小(通常作为指针实现)。

Is infinite loop still undefined behavior in C++ if it calls shared library?

It's said that infinite loop for(;;); is undefined behavior. From http://en.cppreference.com/w/cpp/language/memory_model In a valid C++ program, every thread eventually does one of the following: terminate makes a call to an I/O library function reads or modifies a volatile object performs an atomic operation or a synchronization operation No thread of execution can execute for

如果它调用共享库,C ++中是无限循环还是未定义的行为?

据说for(;;);无限循环for(;;); 是未定义的行为。 从http://en.cppreference.com/w/cpp/language/memory_model 在一个有效的C ++程序中,每个线程最终会执行以下操作之一: 终止 调用I / O库函数 读取或修改一个易失性对象 执行原子操作或同步操作 没有任何执行线程可以执行永远不执行任何这些可观察到的行为。 请注意,这意味着具有无限递归或无限循环的程序(无论是作为for语句还是通过循环goto或其他方式实现

Why is a `fork()` call not optimized away in an infinite loop?

Considering C++11 §1.10/24 (in [intro.multithread]) ” The implementation may assume that any thread will eventually do one of the following: — terminate, — make a call to a library I/O function, — access or modify a volatile object, or — perform a synchronization operation or an atomic operation. [Note: This is intended to allow compiler transformations such as removal of empty loop

为什么`fork()`调用没有在无限循环中被优化掉?

考虑到 C ++ 11§1.10/ 24(in [intro.multithread]) “实现可能会假设任何线程最终都会执行以下操作之一: - 终止, - 拨打图书馆I / O功能, - 访问或修改易失性对象,或 - 执行同步操作或原子操作。 [注意:这是为了允许编译器转换,例如删除空循环,即使无法证实终止。 - 注意] ...是编译器允许优化掉以下循环: int main(int argc, char* argv[]) { while ( true ) fork(); } ? (有

Does the C++ volatile keyword introduce a memory fence?

I understand that volatile informs the compiler that the value may be changed, but in order to accomplish this functionality, does the compiler need to introduce a memory fence to make it work? From my understanding, the sequence of operations on volatile objects cannot be reordered and must be preserved. This seems to imply some memory fences are necessary and that there isn't really a wa

C ++ volatile关键字是否引入了内存围栏?

我明白volatile告诉编译器该值可能会改变,但为了完成这个功能,编译器是否需要引入一个内存栏来使其工作? 根据我的理解,对易失性对象的操作顺序不能重新排序并且必须保留。 这似乎意味着一些记忆栅栏是必要的,并且没有真正解决这个问题的方法。 我说得对吗? 在这个相关的问题上有一个有趣的讨论 乔纳森Wakely写道: ......只要它们出现在单独的完整表达式中,对编译器的访问就不能被编译器重新排序...正确的说,v

Fences in C++0x, guarantees just on atomics or memory in general

The C++0x draft has a notion of fences which seems very distinct from a CPU/chip level notion of fences, or say what the linux kernel guys expect of fences. The question is whether the draft really implies an extremely restricted model, or the wording is just poor and it actually implies true fences. For example, under 29.8 Fences it states things like: A release fence A synchronizes with an

在C ++ 0x中设置栅栏,一般可以保证原子或内存

C ++ 0x草案有一个围栏的概念,它似乎与围墙的CPU /芯片级别概念截然不同,或者说出Linux内核人员对围墙的期望。 问题在于草案是否真的意味着一个非常有限的模式,或者措辞只是很差,实际上意味着真正的围墙。 例如,根据29.8 Fences,它会说明如下内容: 如果存在对原子对象M进行操作的原子操作X和Y,则释放栅栏A与获取栅栏B同步,使得A在X之前被排序,X对M进行排序,Y在B之前排序,并且Y读取值如果是释放操作,则由X书写

Compare and swap C++0x

From the C++0x proposal on C++ Atomic Types and Operations: 29.1 Order and Consistency [atomics.order] Add a new sub-clause with the following paragraphs. The enumeration memory_order specifies the detailed regular (non-atomic) memory synchronization order as defined in [the new section added by N2334 or its adopted successor] and may provide for operation ordering. Its enumerated values a

比较和交换C ++ 0x

从C ++原子类型和操作的C ++ 0x建议: 29.1顺序和一致性[atomics.order] 添加一个新的子条款与以下段落。 枚举memory_order指定了[由N2334或其采用的后继者添加的新节]中定义的详细的常规(非原子)内存同步顺序,并且可以提供操作顺序。 其枚举值及其含义如下。 memory_order_relaxed 该操作不会对内存进行排序。 memory_order_release 对受影响的内存位置执行释放操作,从而通过它所应用的原子变量使其他线程可