File: future.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (143 lines) | stat: -rw-r--r-- 7,300 bytes parent folder | download | duplicates (2)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Condition variables allow threads to wait until data have obtained certain
values. A thread may also have to wait until a sub-thread has finished when
calling a sub-thread's tt(join) member. 

Waiting may be unwelcome: instead of just waiting our thread might also be
doing something useful. It might as well pick up the results produced by a
sub-thread at some point in the future. 

In fact, exchanging data among threads always poses some difficulties, as it
requires shared variables, and the use of locks and mutexes to prevent data
corruption. Rather than waiting and using locks it would be nice if some
asynchronous task could be started, allowing the initiating thread (or even
other threads) to pick up the result at some point in the future, when the
results are needed, without having to worry about data locks or waiting times.
For situations like these bf(C++) provides the class tt(std::future).

Before using the class hi(future) tt(std::future) the tthi(future) header file
must be included.

Objects of the class template hi(future)tt(std::future) harbor the results
produced by asynchronously executed tasks. The class tt(std::future) is a
class template. Its template type parameter specifies the type of the result
returned by the asynchronously executed task. This type may be tt(void).

On the other hand, the asynchronously executed task may throw an exception
(ending the task). In that case the tt(future) object catches the exception,
and rethrows it once its return value (i.e., the value returned by the
asynchronously executed task) is requested.

In this section the members of the class template tt(future) are
described. tt(Future) objects are commonly initialized through anonymous
tt(future) objects returned by the factory function tt(std::async) or by the
tt(get_future) members of the classes tt(std::promise), and
tt(std::packaged_task) (introduced in upcoming sections). Examples of the use
of tt(std::future) objects are provided in those sections.

Some of tt(future)'s members return a value of the strongly typed
enumeration hi(future_status)tt(std::future_status). This enumeration defines
three symbolic constants: tt(future_status::ready, future_status::timeout,)
and tt(future_status::deferred). 

Error conditions are returned through tt(std::future_error)
exceptions. These error conditions are represented by the values of the
strongly typed enumeration tt(std::future_errc) (covered in the next section).

The class tt(future) itself provides the following constructors:
    itemization(
    ittq(future())
       (The default constructor constructs an tt(future) object that does not
        refer to shared results. Its tt(valid) member returns tt(false).)

    ittq(future(future &&tmp) noexcept) 
       (The move constructor is available. Its tt(valid) member returns what
        tt(tmp.valid()) would haved returned prior to the constructor
        invocation. After calling the move constructor tt(tmp.valid()) returns
        tt(false).)
    )
    The class tt(future) does not offer a copy constructor or an overloaded
assignment operator. 

    COMMENT(
 (member functions of the
class tt(future) do not synchronize with themselves or with member functions
of the class tt(shared_future)).
    END)

Here are the members of the class tt(std::future):
    itemization(
    ittq(future &operator=(future &&tmp))
       (The move assignment operator grabs the information from the tt(tmp)
        object; following this, tt(tmp.valid()) returns tt(false).)

    ittq(std::shared_future<ResultType> share() &&)
       (Returns a tt(std::shared_future<ResultType>) (see section
        ref(SHAREDFUTURE)). After calling this function, the tt(future's
        valid) member returns tt(false).)

    ittq(ResultType get())
       (First tt(wait) (see below) is called. Once tt(wait) has returned the
        results produced by the associated asynchronous task
        are returned. With tt(future<Type>) specifications the returned value
        is the moved shared value if tt(Type) supports move assignment,
        otherwise a copy is returned.  With tt(future<Type &>) specifications
        a tt(Type &) is returned, with tt(future<void>) specifications nothing
        is returned. If the shared value is an exception, it is thrown instead
        of returned. After calling this member the tt(future) object's
        tt(valid) member returns tt(false).)

    ittq(bool valid() const) 
       (Returns tt(true) if the (tt(future)) object for which tt(valid) is
        called refers to an object returned by an asynchronous task.  If
        tt(valid) returns tt(false), the tt(future) object exists, but in
        addition to tt(valid) only its destructor and move constructor can
        safely be called. When other members are called while tt(valid)
        returns tt(false) a hi(future_error)tt(std::future_error) exception is
        thrown (having the value hi(no_state)tt(future_errc::no_state)).)

    ittq(void wait() const)
       (The thread is blocked until the results produced by the associated
        asynchronous task are available.)

    ittq(std::future_status wait_for(chrono::duration<Rep, Period> const
        &rel_time) const)
       (This member template derives the template types tt(Rep) and tt(Period)
        from the actually specified duration (cf. section ref(DURATION)). If
        the results contain a deferred function nothing happens. Otherwise
        tt(wait_for) blocks
COMMENT((30.6.8)) 
        until the results are available or until the amount of time
        specified by tt(rel_time) has expired. Possible return values are:
            itemization(
            itt(future_status::deferred) if the results contains a
                deferred function;
            itt(future_status::ready) if the results are avaiable;
            itt(future_status::timeout) if the function is returning because
                the amount of time specified by tt(rel_time) has expired. 
            ))

    ittq(future_status wait_until(chrono::time_point<Clock, Duration> const
                                                        &abs_time) const)
       (This member template derives the template types tt(Clock) and
        tt(Duration) from the actually specified tt(abs_time) (cf. section
        ref(TIMEPOINT)). If the results contain a deferred function nothing
        happens. Otherwise tt(wait_until) blocks until the results are
        available or until the point in time specified by tt(abs_time) has
        expired. Possible return values are:
           itemization(
            itt(future_status::deferred) if the results contain a
                deferred function;
            itt(future_status::ready) if the results are available;
            itt(future_status::timeout) if the function is returning because
                the point in time specified by tt(abs_time) has expired. 
            ))
    )
    The class tt(std::future<ResultType>) declares the following friends:
        verb(    std::promise<ResultType>)

(sf. section ref(PROMISE)), and
        verb(    template<typename Function, typename... Args>
        std::future<typename result_of<Function(Args...)>::type> 
        std::async(std::launch, Function &&fun, Args &&...args);)

(cf. section ref(ASYNC)).