A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/future/packaged_task/reset/ below:

public member function

<future>

std::packaged_task::reset

Reset task

Resets the object with a new shared state while keeping the same stored task.

This allows the stored task to be called again.

The shared state associated to the object before the call (if any) is abandoned (as if the packaged_task was destroyed).

Internally, the function behaves as if move-assigned a newly constructed packaged_task (with its stored task as argument).



Parameters none

Return value none

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// packaged_task::get_future
#include <iostream>     // std::cout
#include <utility>      // std::move
#include <future>       // std::packaged_task, std::future
#include <thread>       // std::thread

// a simple task:
int triple (int x) { return x*3; }

int main ()
{
  std::packaged_task<int(int)> tsk (triple); // package task

  std::future<int> fut = tsk.get_future();
  tsk(33);
  std::cout << "The triple of 33 is " << fut.get() << ".\n";

  // re-use same task object:
  tsk.reset();
  fut = tsk.get_future();
  std::thread(std::move(tsk),99).detach();
  std::cout << "Thre triple of 99 is " << fut.get() << ".\n";

  return 0;
}

Output:
The triple of 33 is 99.
The triple of 99 is 297.


Data races The packaged_task is modified.
The shared state is modified as an atomic operation (causing no data races).

Exception safetyBasic guarantee: if an exception is thrown, the packaged_task is in a valid state.

This member function throws an exception on the following conditions:


This member function also throws if the move constructor of the stored task throws and -depending on the library implementation- may also throw to report other situations.

See also
packaged_task::packaged_task
Construct packaged task (public member function)
packaged_task::operator=
Move-assign packaged_task (public member function)
packaged_task::operator()
Call stored task (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4