Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::packaged_task

      From cppreference.com
      <cpp‎ |thread
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Condition variables
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      (C++20)
      Futures
      (C++11)
      (C++11)
      packaged_task
      (C++11)
      (C++11)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
       
      Defined in header<future>
      template<class>
      class packaged_task;
      (1)(since C++11)
      (not defined)
      template<class R,class ...ArgTypes>
      class packaged_task<R(ArgTypes...)>;
      (2)(since C++11)

      The class templatestd::packaged_task wraps anyCallable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously. Its return value or exception thrown is stored in a shared state which can be accessed throughstd::future objects.

      Just likestd::function,std::packaged_task is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator.

      (until C++17)

      Contents

      [edit]Member functions

      constructs the task object
      (public member function)[edit]
      destructs the task object
      (public member function)[edit]
      moves the task object
      (public member function)[edit]
      checks if the task object has a valid function
      (public member function)[edit]
      swaps two task objects
      (public member function)[edit]
      Getting the result
      returns astd::future associated with the promised result
      (public member function)[edit]
      Execution
      executes the function
      (public member function)[edit]
      executes the function ensuring that the result is ready only once the current thread exits
      (public member function)[edit]
      resets the state abandoning any stored results of previous executions
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function template)[edit]

      [edit]Helper classes

      specializes thestd::uses_allocator type trait
      (class template specialization)[edit]

      [edit]Deduction guides(since C++17)

      [edit]Example

      Run this code
      #include <cmath>#include <functional>#include <future>#include <iostream>#include <thread> // unique function to avoid disambiguating the std::pow overload setint f(int x,int y){returnstd::pow(x, y);} void task_lambda(){    std::packaged_task<int(int,int)> task([](int a,int b){returnstd::pow(a, b);});std::future<int> result= task.get_future();     task(2,9); std::cout<<"task_lambda:\t"<< result.get()<<'\n';} void task_bind(){    std::packaged_task<int()> task(std::bind(f,2,11));std::future<int> result= task.get_future();     task(); std::cout<<"task_bind:\t"<< result.get()<<'\n';} void task_thread(){    std::packaged_task<int(int,int)> task(f);std::future<int> result= task.get_future(); std::thread task_td(std::move(task),2,10);    task_td.join(); std::cout<<"task_thread:\t"<< result.get()<<'\n';} int main(){    task_lambda();    task_bind();    task_thread();}

      Output:

      task_lambda: 512task_bind:   2048task_thread: 1024

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3117C++17deduction guides forpackaged_task were missingadded

      [edit]See also

      (C++11)
      waits for a value that is set asynchronously
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/packaged_task&oldid=180411"

      [8]ページ先頭

      ©2009-2025 Movatter.jp