Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/promise"

From cppreference.com
< cpp‎ | thread
m (fmt, capitalized 1st letter, ., headers sorted)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
#include<iostream>
+
{{cpp/title|promise}}
#include<cstdio>
+
{{cpp/thread/promise/navbar}}
#include<string>
+
{{dcl begin}}
 +
{{dcl header|future}}
 +
{{dcl|num=1|since=c++11|
 +
template< class R > class promise;
 +
}}
 +
{{dcl|num=2|since=c++11|
 +
template< class R > class promise<R&>;
 +
}}
 +
{{dcl|num=3|since=c++11|
 +
template<> class promise<void>;
 +
}}
 +
{{dcl end}}
  
//zadanie1
+
@1@ Base template.
using namespace std;
+
@2@ Non-void specialization, used to communicate objects between threads.
double fun_aux(double , double , double );
+
@3@ void specialization, used to communicate stateless events.
  
void fun_arr(double arr1[], double arr2[])
+
The class template {{tt|std::promise}} provides a facility to store a value or an exception that is later acquired asynchronously via a {{lc|std::future}} object created by the {{tt|std::promise}} object. Note that the {{tt|std::promise}} object is meant to be used only once.
{
+
int k = 0;
+
  
for (int i = 0; i < 12; i += 3)
+
Each promise is associated with a ''shared state'', which contains some state information and a ''result'' which may be not yet evaluated, evaluated to a value (possibly void) or evaluated to an exception. A promise may do three things with the shared state:
{
+
* ''make ready'': the promise stores the result or the exception in the shared state. Marks the state ready and unblocks any thread waiting on a future associated with the shared state.
/*printf("%4.1f ", arr1[i]);
+
* ''release'': the promise gives up its reference to the shared state. If this was the last such reference, the shared state is destroyed. Unless this was a shared state created by {{c|std::async}} which is not yet ready, this operation does not block.
k++;
+
* ''abandon'': the promise stores the exception of type {{lc|std::future_error}} with error code {{lc|std::future_errc::broken_promise}}, makes the shared state ''ready'', and then ''releases'' it.
if (k % 3 == 0 ) cout << endl;*/
+
+
arr2[k] = fun_aux(arr1[i], arr1[i + 1], arr1[i + 2]);
+
//printf("%5.2f ", arr2[k]);
+
k++;
+
}
+
cout << endl;
+
}
+
  
double fun_aux(double a1, double a2, double a3)
+
The promise is the "push" end of the promise-future communication channel: the operation that stores a value in the shared state ''synchronizes-with'' (as defined in {{lc|std::memory_order}}) the successful return from any function that is waiting on the shared state (such as {{lc|std::future::get}}). Concurrent access to the same shared state may conflict otherwise: for example multiple callers of {{lc|std::shared_future::get}} must either all be read-only or provide external synchronization.
{
+
double max = a1;
+
if (max < a2) max = a2;
+
if (max < a3) max = a3;
+
+
  
return max;
+
===Member functions===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/thread/promise/dsc constructor}}
 +
{{dsc inc|cpp/thread/promise/dsc destructor}}
 +
{{dsc inc|cpp/thread/promise/dsc operator{{=}}}}
 +
{{dsc inc|cpp/thread/promise/dsc swap}}
 +
 
 +
{{dsc h2|Getting the result}}
 +
{{dsc inc|cpp/thread/promise/dsc get_future}}
 +
 
 +
{{dsc h2|Setting the result}}
 +
{{dsc inc|cpp/thread/promise/dsc set_value}}
 +
{{dsc inc|cpp/thread/promise/dsc set_value_at_thread_exit}}
 +
{{dsc inc|cpp/thread/promise/dsc set_exception}}
 +
{{dsc inc|cpp/thread/promise/dsc set_exception_at_thread_exit}}
 +
{{dsc end}}
 +
 
 +
===Non-member functions===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/thread/promise/dsc swap2}}
 +
{{dsc end}}
 +
 
 +
===Helper classes===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/thread/promise/dsc uses_allocator}}
 +
{{dsc end}}
 +
 
 +
===Example===
 +
{{example
 +
|This example shows how {{tt|promise<int>}} can be used as signals between threads.
 +
|code=
 +
#include <chrono>
 +
#include <future>
 +
#include <iostream>
 +
#include <numeric>
 +
#include <thread>
 +
#include <vector>
 +
 +
void accumulate(std::vector<int>::iterator first,
 +
                std::vector<int>::iterator last,
 +
                std::promise<int> accumulate_promise)
 +
{
 +
    int sum = std::accumulate(first, last, 0);
 +
    accumulate_promise.set_value(sum); // Notify future
 
}
 
}
  
 +
void do_work(std::promise<void> barrier)
 +
{
 +
    std::this_thread::sleep_for(std::chrono::seconds(1));
 +
    barrier.set_value();
 +
}
 +
 
int main()
 
int main()
 
{
 
{
int n = 4;
+
    // Demonstrate using promise<int> to transmit a result between threads.
double* arr1 = new double[3 * n];
+
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
double* arr2 = new double[n];
+
    std::promise<int> accumulate_promise;
for ( int i = 0; i < n; i++)
+
    std::future<int> accumulate_future = accumulate_promise.get_future();
{
+
    std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
arr2[i] = 0;
+
                            std::move(accumulate_promise));
}
+
double arr[] = {1.0, 1.0, 2.5, 2.0, 1.0, -0.5, -1.5, -3.0,-4.0, 0.0, 0.0, 0.0, 0.0};
+
for (int i = 0; i < 3*n; i++)
+
{
+
arr1[i] = arr[i];
+
}
+
fun_arr(arr1, arr2);
+
for ( int i = 0; i < n; i++)
+
{
+
printf("%5.2f ", arr2[i]);
+
}
+
cout << endl;
+
  
//fun_arr(arr1, &arr2);
+
    // future::get() will wait until the future has a valid result and retrieves it.
+
    // Calling wait() before get() is not needed
delete[] arr2;
+
    // accumulate_future.wait(); // wait for result
delete[] arr1;
+
    std::cout << "result=" << accumulate_future.get() << '\n';
return 0;
+
    work_thread.join(); // wait for thread completion
 +
   
 +
    // Demonstrate using promise<void> to signal state between threads.
 +
    std::promise<void> barrier;
 +
    std::future<void> barrier_future = barrier.get_future();
 +
    std::thread new_work_thread(do_work, std::move(barrier));
 +
    barrier_future.wait();
 +
    new_work_thread.join();
 
}
 
}
 +
|output=
 +
result=21
 +
}}
 +
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 02:19, 23 October 2023

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
promise
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
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
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <future>
template< class R > class promise;
(1) (since C++11)
template< class R > class promise<R&>;
(2) (since C++11)
template<> class promise<void>;
(3) (since C++11)
1) Base template.
2) Non-void specialization, used to communicate objects between threads.
3) void specialization, used to communicate stateless events.

The class template std::promise provides a facility to store a value or an exception that is later acquired asynchronously via a std::future object created by the std::promise object. Note that the std::promise object is meant to be used only once.

Each promise is associated with a shared state, which contains some state information and a result which may be not yet evaluated, evaluated to a value (possibly void) or evaluated to an exception. A promise may do three things with the shared state:

  • make ready: the promise stores the result or the exception in the shared state. Marks the state ready and unblocks any thread waiting on a future associated with the shared state.
  • release: the promise gives up its reference to the shared state. If this was the last such reference, the shared state is destroyed. Unless this was a shared state created by std::async which is not yet ready, this operation does not block.
  • abandon: the promise stores the exception of type std::future_error with error code std::future_errc::broken_promise, makes the shared state ready, and then releases it.

The promise is the "push" end of the promise-future communication channel: the operation that stores a value in the shared state synchronizes-with (as defined in std::memory_order) the successful return from any function that is waiting on the shared state (such as std::future::get). Concurrent access to the same shared state may conflict otherwise: for example multiple callers of std::shared_future::get must either all be read-only or provide external synchronization.

Contents

[edit] Member functions

constructs the promise object
(public member function) [edit]
destructs the promise object
(public member function) [edit]
assigns the shared state
(public member function) [edit]
swaps two promise objects
(public member function) [edit]
Getting the result
returns a future associated with the promised result
(public member function) [edit]
Setting the result
sets the result to specific value
(public member function) [edit]
sets the result to specific value while delivering the notification only at thread exit
(public member function) [edit]
sets the result to indicate an exception
(public member function) [edit]
sets the result to indicate an exception while delivering the notification only at thread exit
(public member function) [edit]

[edit] Non-member functions

specializes the std::swap algorithm
(function template) [edit]

[edit] Helper classes

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

[edit] Example

This example shows how promise<int> can be used as signals between threads.

#include <chrono>
#include <future>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
 
void accumulate(std::vector<int>::iterator first,
                std::vector<int>::iterator last,
                std::promise<int> accumulate_promise)
{
    int sum = std::accumulate(first, last, 0);
    accumulate_promise.set_value(sum); // Notify future
}
 
void do_work(std::promise<void> barrier)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    barrier.set_value();
}
 
int main()
{
    // Demonstrate using promise<int> to transmit a result between threads.
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();
    std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
                            std::move(accumulate_promise));
 
    // future::get() will wait until the future has a valid result and retrieves it.
    // Calling wait() before get() is not needed
    // accumulate_future.wait(); // wait for result
    std::cout << "result=" << accumulate_future.get() << '\n';
    work_thread.join(); // wait for thread completion
 
    // Demonstrate using promise<void> to signal state between threads.
    std::promise<void> barrier;
    std::future<void> barrier_future = barrier.get_future();
    std::thread new_work_thread(do_work, std::move(barrier));
    barrier_future.wait();
    new_work_thread.join();
}

Output:

result=21