本文整理汇总了C++中std::promise类的典型用法代码示例。如果您正苦于以下问题:C++ promise类的具体用法?C++ promise怎么用?C++ promise使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了promise类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: finish
virtual void finish(error_code code, http::status status) {
m_message_accum.error_code = code;
m_message_accum.error_string = error_buffer;
m_message_accum.status = status;
// call the receive callback a last time with the final
// error code
if(m_on_receive) {
assert(m_message_accum.body.empty());
m_on_receive(m_message_accum, progress());
}
m_send_file.reset();
m_receive_file.reset();
// set the final message data so it can be retrieved
// with the message-future object in the response object
m_message_promise.set_value(std::move(m_message_accum));
// call an optional continuation callback for this request
if(m_on_finish) {
m_on_finish();
}
// mark this request as finished
finished_promise.set_value();
}
示例2: op
void op() // GB was operator
{
try {
promise.set_value(f());
} catch(...) {
promise.set_exception(std::current_exception());
}
}
示例3: f
shared_fence(const vk::vk_logical_device<> &device,
T &&val,
const char *name,
typename std::enable_if<!std::is_void<S>::value>::type* = nullptr)
: f(device,
name,
true),
future(promise.get_future().share())
{
promise.set_value(std::forward<T>(val));
}
示例4: doSomething
void doSomething (std::promise<std::string>& p)
{
try {
// read character and throw exception if 'x'
std::cout << "read char ('x' for exception): ";
char c = std::cin.get();
if (c == 'x') {
throw std::runtime_error(std::string("char ")+c+" read");
}
//...
std::string s = std::string("char ") + c + " processed";
p.set_value(std::move(s)); // store result
}
catch (...) {
p.set_exception(std::current_exception()); // store exception
}
}
示例5: operator
void operator()() {
osmium::thread::set_thread_name("_osmium_write");
try {
while (true) {
std::string data = m_queue.pop();
if (at_end_of_data(data)) {
break;
}
m_compressor->write(data);
}
m_compressor->close();
m_promise.set_value(true);
} catch (...) {
m_promise.set_exception(std::current_exception());
m_queue.drain();
}
}
示例6: set
FM<T>& set(SM<T, Ts...>& input, const bool is_lazy, Ts&... args) {
_promise = std::promise<T>();
_future = _promise.get_future();
auto f = std::async(
is_lazy ? std::launch::deferred : std::launch::async,
[&] () {
task_loop<T, Ts...>()(input, _promise, args...);
}
);
if(is_lazy) f.get();
return *this;
};
示例7: correlation_parallel_helper
void correlation_parallel_helper(VD::const_iterator first,
VD::const_iterator last,
std::promise<corr_intermediate> accumulate_promise)
{
const size_t N = last - first;
corr_intermediate result;
result.sum = std::accumulate(first, last, 0);
result.mean = result.sum / N;
result.sumsqr = std::inner_product(first, last, first, 0);
result.stdev = N > 1 ? sqrt((result.sumsqr - result.sum * result.sum / N) / (N - 1)) : 0;
if (result.stdev) {
result.Y.reserve(N);
std::transform(first, last, result.Y.begin(), [result](double val) { return (val - result.mean)/result.stdev; });
}
accumulate_promise.set_value(result); // Notify future
}
示例8: backtrackSolve
bool backtrackSolve(bool (*board)[8][8], unsigned short vPos, std::promise<bool>& p)
{
if(vPos == 8) // catch for already complete board
return true;
for(unsigned short hPos = 0; hPos < 8; hPos++)
if(singleConflictCheck(board, hPos, vPos))
{
(*board)[hPos][vPos] = 1;
if(backtrackSolve(board, vPos + 1, p) == true) //return chain to escape all recursively called functions.
{
if(vPos == 0) //only run on initial function call
p.set_value_at_thread_exit(true); //setting thread complete status on thread exit
return true;
}
(*board)[hPos][vPos] = 0;
}
return false;
}
示例9: func5
void func5(std::promise<void> p)
{
std::this_thread::sleep_for(ms(500));
p.set_value();
}
示例10: function
inline void ExecuteAndSetValue<void>(std::function<void()> const& function,
std::promise<void>& promise) {
function();
promise.set_value();
}
示例11: ExecuteAndSetValue
void ExecuteAndSetValue(std::function<T()> const& function,
std::promise<T>& promise) {
promise.set_value(function());
}
示例12: func3
void func3(std::promise<int&> p)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
j = 5;
p.set_value(j);
}
示例13: reset
/**
* @brief Resets fence to unsignaled state
* Not thread-safe.
*/
void reset() override {
f.reset();
promise = std::promise<R>();
future = promise.get_future().share();
}
示例14: func4
void func4(std::promise<int&> p)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
p.set_exception(std::make_exception_ptr(3.5));
}
示例15: func5
void func5(std::promise<void> p)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
p.set_value();
}