15 Eylül 2017 Cuma

thread future Sınıfı

Future Sınıfı
Giriş
Şu satırı dahil ederiz.
#include <boost/thread/future.hpp>
Bu sınıfı kullanabilmek için şu macro tanımının yapılması gerekebilir.
#define BOOST_THREAD_PROVIDES_FUTURE
Constructor
Örnek
Şöyle yaparız.
boost::future<std::pair<std::string, int>>> f = boost::async ([]() {
  ...
  return std::make_pair (..., ...);
});
Örnek
Şöyle yaparız
boost::future<int> f= boost::async(boost::launch::async, boost::bind(fun, arg));
get metodu
Şöyle yaparız.
const auto res = f.get();
then metodu
Örnek
Şöyle yaparız.
auto f = boost::make_ready_future<int>();

boost::loop_executor ex;

auto a = f.then (ex, [](auto &&) {...}).share();
Örnek
Şöyle yaparız
boost::future<void> on_finish = f.then(boost::launch::deferred, []() {
  ...
});
unwrap metodu
Açıklaması şöyle
It is a common situation that the body of a then function object will itself be a future-based operation, which leads to the then() returning a future>. In this case, it is almost always the case that what you really care about is the inner future, so then() performs an implicit unwrap() (see below) before returning.
C++ önerisindeki kullanım şekli şöyle
auto promise = std::promise<int>{};
auto another_future = promise.get_future().then([](auto future) {
    return std::async([]() { return 1; });
});
then metodunun sonucunu std::future<std::future<int>> yerine std::future<int> olarak almak için şöyle yaparız.
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread.hpp>

int main() {
  auto promise = boost::promise<int>{};
  boost::future<int> another_future = 
    promise.get_future().then([](auto) {
      return boost::async([]() { return 1; });
    }).unwrap();
}
Free Style Metodlar
make_ready_future metodu
Şöyle yaparız.
auto f = boost::make_ready_future<int>();
wait_for_any metodu
Şöyle yaparız.
using vbf = std::vector<boost::future<std::pair<std::string, int>>>;
vbf tasks = ...;
auto it = boost::wait_for_any (tasks.begin(), tasks.end());
when_all metodu
Şöyle yaparız.
boost::loop_executor ex;

auto f = ...;

auto b = boost::when_all (f).then(ex, [](auto &&) {...} );





Hiç yorum yok:

Yorum Gönder