帮忙贴下您的运行结果

turing-complete 2013-11-26 09:54:33
顺便说明下您使用的操作系统和编译器及其版本,谢谢了。

#include <chrono>
#include <future>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>

std::mutex g_display_mutex;

void PrintThreadId() {
std::this_thread::sleep_for(std::chrono::seconds(2));

{
std::lock_guard<std::mutex> lock(g_display_mutex);
std::cout << std::this_thread::get_id() << std::endl;
}
}

int main(int argc, char* argv[]) {
std::vector<std::future<void>> futures;

for (int i = 0; i < 10; ++i) {
futures.emplace_back(std::async([]{
return PrintThreadId();
}));
}

for (std::future<void>& future : futures) {
future.wait();
}
}
...全文
474 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2013-11-28
  • 打赏
  • 举报
回复
引用 15 楼 mougaidong 的回复:
谢谢大家,我只想看看这个程序的运行结果,不是不知道怎么异步和同步。 我以为在有的平台上会出现异步同步间或出现的情况,例如运行结果是: id1 id2 id3 id4 id5 id6 id1 id2 id3 id4 看来这个std::async是鸡肋了点,在生产环境中不太敢用。 [quote=引用 13 楼 ri_aje 的回复:]
引用 14 楼 dyw 的回复:
[/quote] 我觉得这版的多线程支持设计的确是不太好,比如 thread 需要自己 join 或 detach,要是忘记了,析构函数也不管,直接死程序,不知道委员会怎么想的。
turing-complete 2013-11-27
  • 打赏
  • 举报
回复
大师,您深奥而独到见解,跟我们这些俗人怎么讲的通呢。 与我们为伍,不是在糟践您自己嘛。
引用 9 楼 zhao4zhong1 的回复:
把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
SKATE11 2013-11-27
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
http://codepad.org/9enwhB7c Line 17: error: chrono: No such file or directory Line 17: error: future: No such file or directory Line 16: error: mutex: No such file or directory Line 17: error: thread: No such file or directory Line 8: error: 'mutex' in namespace 'std' does not name a type compilation terminated due to -Wfatal-errors.
+++
赵4老师 2013-11-27
  • 打赏
  • 举报
回复
把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
  • 打赏
  • 举报
回复
win7 vs2012 update 3 1884 1976 1472 3904 3436 3724 3076 2644 1760 1928
turing-complete 2013-11-27
  • 打赏
  • 举报
回复
贴一下我自己这里的运行结果,环境:RHEL6.3 & g++4.8.2 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456 140192484763456
line_us 2013-11-27
  • 打赏
  • 举报
回复
看看各种运行结果。
turing-complete 2013-11-27
  • 打赏
  • 举报
回复
谢谢大家,我只想看看这个程序的运行结果,不是不知道怎么异步和同步。 我以为在有的平台上会出现异步同步间或出现的情况,例如运行结果是: id1 id2 id3 id4 id5 id6 id1 id2 id3 id4 看来这个std::async是鸡肋了点,在生产环境中不太敢用。
引用 13 楼 ri_aje 的回复:
引用 14 楼 dyw 的回复:
turing-complete 2013-11-27
  • 打赏
  • 举报
回复
引用 1 楼 Vegertar 的回复:

vegertar@mac:/tmp$ uname -a
Darwin mac.vegertar 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
vegertar@mac:/tmp$ c++ -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
vegertar@mac:/tmp$ c++ -std=c++11 a.cc
vegertar@mac:/tmp$ ./a.out
0x10e4b2000
0x10e42f000
0x10e3ac000
0x10e535000
0x10e5b8000
0x10e63b000
0x10e6be000
0x10e741000
0x10e7c4000
0x10e847000

看来,这个总是 std::launch::async
dyw 2013-11-27
  • 打赏
  • 举报
回复
mingw4.8.0 ( 不使用std::launch::async )
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
mingw4.8.0 (使用std::launch::async)
5: 7
3: 5
7: 9
1: 3
9: 11
2: 4
0: 2
4: 6
6: 8
8: 10
我稍微改了一下代码,把i值传入线程里了。冒号前面为i值,后面为ThreadId。
FancyMouse 2013-11-27
  • 打赏
  • 举报
回复
2008R2 VS2013 Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64 D:\temp>b.exe 9396 9044 1116 14016 5584 10320 11916 19536 13688 11116
ri_aje 2013-11-27
  • 打赏
  • 举报
回复
ubuntu 10.04 g++-4.8.0

3077957328
3077957328
3077957328
3077957328
3077957328
3077957328
3077957328
3077957328
3077957328
3077957328
想要多线程的话,必须这样写。

futures.emplace_back(std::async(std::launch::async, []{
 return PrintThreadId();
}));
再次运行的话,输出如下。

3078294384
3069901680
3059411824
3034233712
3042626416
3051019120
3017448304
3025841008
3009055600
3000662896
3078301392
注意 async 的第一个参数需要明确指明 launch::async。这是因为 c++11 30.6.8/3 states The first function behaves the same as a call to the second function with a policy argument of launch::async | launch::deferred and the same arguments for F and Args. ... The further behavior of the second function depends on the policy argument as follows (if more than one of these conditions applies, the implementation may choose any of the corresponding policies). 因此 async(...) == async(launch::async | launch::deferred, ...); 并且实现可以自由选择 async 方式,或 deferred 方式。 该段同时定义了两种方式的行为,见下。 if policy & launch::async is non-zero, calls INVOKE(DECAY_COPY (std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...) (20.8.2, 30.3.1.2) as if in a new thread of execution represented by a thread object with the calls to DECAY_COPY () being evaluated in the thread that called async. 同时 if policy & launch::deferred is non-zero, ..., The first call to a non-timed waiting function (30.6.4) on an asynchronous return object referring to this shared state shall invoke the deferred function in the thread that called the waiting function. 所以,g++ 选择同一线程以及其它实现选择多线程都符合标准,实际上 30.6.8/9 特地说明了这个问题。 要想确保多线程执行,用户必须自己说清楚。
Sky丶Memory 2013-11-27
  • 打赏
  • 举报
回复
GCC 4.7.1下面 main.cpp:8:1: error: 'mutex' in namespace 'std' does not name a type main.cpp: In function 'void PrintThreadId()': main.cpp:11:8: error: 'std::this_thread' has not been declared main.cpp:14:5: error: 'lock_guard' is not a member of 'std' main.cpp:14:21: error: 'mutex' is not a member of 'std' main.cpp:14:38: error: 'g_display_mutex' was not declared in this scope main.cpp:14:53: error: 'lock' was not declared in this scope main.cpp:15:23: error: 'std::this_thread' has not been declared main.cpp: In function 'int main(int, char**)': main.cpp:25:6: error: invalid use of incomplete type 'std::__async_sfinae_helper <main(int, char**)::<lambda()>, main(int, char**)::<lambda()> >::type {aka class std::future<void>}'
ken_scott 2013-11-26
  • 打赏
  • 举报
回复

>>> c++ -std=c++0x -o ttttt ttttt.cpp 
ttttt.cpp: In function ‘int main(int, char**)’:
ttttt.cpp:28:34: error: expected initializer before ‘:’ token
ttttt.cpp:31:1: error: expected primary-expression before ‘}’ token
ttttt.cpp:31:1: error: expected ‘;’ before ‘}’ token
ttttt.cpp:31:1: error: expected primary-expression before ‘}’ token
ttttt.cpp:31:1: error: expected ‘)’ before ‘}’ token
ttttt.cpp:31:1: error: expected primary-expression before ‘}’ token
ttttt.cpp:31:1: error: expected ‘;’ before ‘}’ token
我的编译器还没升级到支持C++11 。。。
healer_kx 2013-11-26
  • 打赏
  • 举报
回复
我Mac OSX上面Clang++编译运行的。 oughtlast-lm:c++11 zmyu$ clang++ cc.cpp -std=c++11 -stdlib=libc++ oughtlast-lm:c++11 zmyu$ ./a.out 0x1079db000 0x107a5e000 0x107958000 0x107ae1000 0x107b64000 0x107c6a000 0x107be7000 0x107ced000 0x107d70000 0x107df3000 oughtlast-lm:c++11 zmyu$
allenltiverson 2013-11-26
  • 打赏
  • 举报
回复
我表示vc下没有这几个头文件 #include <chrono> #include <future> #include <thread> #include <mutex>
threenewbee 2013-11-26
  • 打赏
  • 举报
回复
http://codepad.org/9enwhB7c Line 17: error: chrono: No such file or directory Line 17: error: future: No such file or directory Line 16: error: mutex: No such file or directory Line 17: error: thread: No such file or directory Line 8: error: 'mutex' in namespace 'std' does not name a type compilation terminated due to -Wfatal-errors.
Vegertar 2013-11-26
  • 打赏
  • 举报
回复

vegertar@mac:/tmp$ uname -a
Darwin mac.vegertar 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
vegertar@mac:/tmp$ c++ -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
vegertar@mac:/tmp$ c++ -std=c++11 a.cc
vegertar@mac:/tmp$ ./a.out
0x10e4b2000
0x10e42f000
0x10e3ac000
0x10e535000
0x10e5b8000
0x10e63b000
0x10e6be000
0x10e741000
0x10e7c4000
0x10e847000

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧