Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use std::packaged_task in Category 11

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use std::packaged_task in Craft 11, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use std::packaged_task in Craft 11. Let's take a look.

Std::packaged_task in Category 11 is a template class.

Std::packaged_task wraps any callable target (function, lambda expression, bind expression, function object) so that it can be called asynchronously. Its return value or thrown exception is stored in a shared state that is accessible through the std::future object.

Std::packaged_task is similar to std::function, but automatically passes its results to the std::future object.

The std::packaged_task object contains two elements:

(1)。 Stored tasks (stored task) are callable objects (such as function pointers, members, or pointers to function objects) (A stored task, which is some callable object (such as a function pointer, pointer to member or function object)).

(2)。 Shared state, which can store the results of calling stored tasks (stored task) and can be accessed asynchronously through std::future (A shared state, which is able to store the results of calling the stored task and be accessed asynchronously through a future).

The shared state is associated with the std::future object by calling the get_future member of the std::packaged_task. After the call, the two objects share the same shared state: (1) the std::packaged_task object is an asynchronous provider (asynchronous provider), and the shared state should be set to ready at some point by calling the stored task (stored task).

The .std:: future object is an asynchronous return object that retrieves the value of the shared state and waits for it to be ready if necessary.

The lifetime of a shared state lasts at least until the last object associated with it is released or destroyed.

Std::packaged_task doesn't start itself, you have to call it (A packaged_task won't start on it's own, you have to invoke it).

The template class std::packaged_task member functions include:

1. Constructor:

(1)。 Default constructor: initializes the object with no shared state and no storage task (no shared state and no stored task).

(2)。 Initialization constructor: this object has a shared state and its stored tasks are initialized by fn.

(3)。 Initialization constructor with allocator .

(4)。 Disable copy construction.

(5)。 Mobile construction is supported.

two。 Destructor:

(1)。 Abandon the shared state and destroy the packaged_task object.

(2)。 If there are other future objects associated with the same shared state, the shared state itself is not destroyed.

(3)。 If the packaged_task object is destroyed before the shared state is ready, the shared state is automatically ready and contains an exception of type std::future_error.

3. Get_future function:

(1)。 Returns a std::future object associated with the shared state of the packaged_task object.

(2)。 Once the stored task is called, the returned std::future object can access the value or exception set by the packaged_task object on the shared state.

(3)。 Each packaged_task shared state can only be retrieved by one std::future object (Only one future object can be retrieved for each packaged_task shared state).

(4)。 After calling this function, packaged_task should get its shared state ready at some point (by calling its stored task), otherwise it will be automatically ready after destruction and contain an exception of type std::future_error.

4. The make_ready_at_thread_exit function: ready the shared state only when the thread exits, rather than ready immediately after the call is completed.

5. Operator=:

(1)。 Disable copy assignment.

(2)。 Mobile assignment is supported.

6. Operator ():

(1) call stored task.

(2)。 If the call to the storage task completes successfully or throws an exception, the returned value or caught exception is stored in the shared state, which is ready (unblocking all threads currently waiting for it).

7. Reset function:

(1)。 Resets the object in a new shared state while maintaining the same stored task.

(2)。 Allows the stored task to be called again.

(3)。 The previous shared state associated with the object is discarded (as if the packaged_task had been destroyed).

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

8. Swap function / non-member template function swap: exchange shared state and storage tasks (stored task).

9. The valid function: checks whether the packaged_task object has a shared state.

For more information, please see the following test code. The following is the test code of copy from other articles. Some adjustments have been made. For more information, please refer to the corresponding reference:

# include "future.hpp" # include # include namespace future_ {/ reference: http://www.cplusplus.com/reference/future/packaged_task/int Test_packaged_task_1 () {{/ / constructor/get_future/operator=/valid std::packaged_task foo / / default-constructed std::packaged_task bar ([] (int x) {return x * 2;}); / / initialized foo = std::move (bar); / / move-assignment std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report