C++ 我如何滥用C++;这里的承诺和未来?
这是我的代码,C++ 我如何滥用C++;这里的承诺和未来?,c++,promise,future,C++,Promise,Future,这是我的代码,main.cpp: #include <string> #include <iostream> #include <future> int main() { using namespace std; auto p = promise<string>(); p.set_value("Hello, world. "); auto f = p.get_future(); cout << f.get(
main.cpp
:
#include <string>
#include <iostream>
#include <future>
int main() {
using namespace std;
auto p = promise<string>();
p.set_value("Hello, world. ");
auto f = p.get_future();
cout << f.get() << endl;
return 0;
}
我的编译器版本:
$ clang++ --version
clang version 7.0.0-3 (tags/RELEASE_700/final)
Target: x86_64-pc-linux-gnu
Thread model: posix InstalledDir: /usr/bin
我的编译命令:
$ clang++ ./main.cpp && ./a.out
<代码> STD::承诺< /COD>是C++线程支持的一部分,即使您使用非线程方式。 因此,您必须使用编译器选项
-pthread
:
clang++ -pthread main.cpp
异常在哪里抛出?对我来说正在工作。。。这很有趣。为什么没有链接时间错误?另外,
-pthreads
标志意味着什么?它与-lpthread
有什么不同?@r3musn0x我认为-pthreads
(编译器标志)可能会设置内部编译器宏或其他可能影响标准库头的魔法,而-lpthread
(链接器标志)只会链接库对象。AFAIK,-pthreads
类似于编译器的-D_REENTRANT
和链接器的-lpthread
,但细节可能因体系结构而异。关于为什么链接器没有失败,我不知道,但我认为这可能与pthreads是一个共享对象有关。
clang++ -pthread main.cpp