C++中std::thread的简单返回值如何实现为一个长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计296个文字,预计阅读时间需要2分钟。
使用Win32线程,我拥有直接的GetExitCodeThread()函数,它返回线程函数返回的值。我正在为std::thread(或boost线程)寻找类似的东西。据我所知,这可以通过期货完成,但究竟如何呢?关于C++11期货,请参阅以下内容。
使用win32线程,我有直接的GetExitCodeThread(),它给出了线程函数返回的值.我正在为std :: thread(或boost线程)寻找类似的东西据我所知,这可以通过期货完成,但究竟如何呢? 关于C 11期货,请参见 this video tutorial.
明确与线程和期货:
#include <thread> #include <future> void func(std::promise<int> && p) { p.set_value(1); } std::promise<int> p; auto f = p.get_future(); std::thread t(&func, std::move(p)); t.join(); int i = f.get();
或者使用std :: async(线程和期货的更高级别包装器):
#include <thread> #include <future> int func() { return 1; } std::future<int> ret = std::async(&func); int i = ret.get();
我无法评论它是否适用于所有平台(它似乎适用于Linux,但不适用于Mac OSX和GCC 4.6.1).
本文共计296个文字,预计阅读时间需要2分钟。
使用Win32线程,我拥有直接的GetExitCodeThread()函数,它返回线程函数返回的值。我正在为std::thread(或boost线程)寻找类似的东西。据我所知,这可以通过期货完成,但究竟如何呢?关于C++11期货,请参阅以下内容。
使用win32线程,我有直接的GetExitCodeThread(),它给出了线程函数返回的值.我正在为std :: thread(或boost线程)寻找类似的东西据我所知,这可以通过期货完成,但究竟如何呢? 关于C 11期货,请参见 this video tutorial.
明确与线程和期货:
#include <thread> #include <future> void func(std::promise<int> && p) { p.set_value(1); } std::promise<int> p; auto f = p.get_future(); std::thread t(&func, std::move(p)); t.join(); int i = f.get();
或者使用std :: async(线程和期货的更高级别包装器):
#include <thread> #include <future> int func() { return 1; } std::future<int> ret = std::async(&func); int i = ret.get();
我无法评论它是否适用于所有平台(它似乎适用于Linux,但不适用于Mac OSX和GCC 4.6.1).

