forked from HowardHinnant/papers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththread-raii-solution2.html
149 lines (121 loc) · 4.21 KB
/
thread-raii-solution2.html
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
144
145
146
147
148
149
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Wording for Discussion about std::thread and RAII, Solution 2: Add a new thread type that auto-joins</title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {color:#00A000}
del {color:#A00000}
</style>
</head>
<body>
<address align=right>
Document number: D????
<br/>
<br/>
<a href="mailto:[email protected]">Ville Voutilainen</a><br/>
2016-01-09<br/>
</address>
<hr/>
<h1 align=center>Wording for Discussion about std::thread and RAII, Solution 2: Add a new thread type that auto-joins</h1>
<h2>Solution 2: Add a new thread type that auto-joins</h2>
<p>
In [thread.threads]/1, insert as follows:
</p>
<p>
<blockquote>
<pre>
Header <thread> synopsis
namespace std {
class thread;
<ins>class safe_thread;</ins>
</pre>
</blockquote>
</p>
<p>
After [thread.thread.this], add a new section as follows:
</p>
<p>
<blockquote>
<pre>
Class safe_thread [thread.safe_thread.class]
namespace std {
class safe_thread {
public:
// types:
class id;
typedef implementation-defined native_handle_type; // See 30.2.3
// construct/copy/destroy:
safe_thread() noexcept;
template <class F, class ...Args> explicit safe_thread(F&& f, Args&&... args);
~safe_thread() noexcept; // semantics different from std::thread
safe_thread(const safe_thread&) = delete;
safe_thread(safe_thread&&) noexcept;
safe_thread(thread&& x) noexcept; // addition to std::thread interface
safe_thread& operator=(const safe_thread&) = delete;
safe_thread& operator=(safe_thread&&) noexcept; // semantics different from std::thread
safe_thread& operator=(thread&& x) noexcept; // addition to std::thread interface
// members:
void swap(safe_thread&) noexcept;
bool joinable() const noexcept;
void join();
void detach();
id get_id() const noexcept;
native_handle_type native_handle(); // See 30.2.3
thread to_thread(); // addition to std::thread interface
// static members:
static unsigned hardware_concurrency() noexcept;
};
}
The class safe_thread provides the same facilities as thread, and
has the same members and the same semantics, with the differences
as described below.
safe_thread constructors [thread.safe_thread.constr]
safe_thread(thread&& x) noexcept;
Effects: Constructs an object of type safe_thread from x, and
sets x to a default constructed state.
Postconditions: x.get_id() == id() and get_id() returns the value
of x.get_id() prior to the start of construction.
safe_thread destructor [thread.safe_thread.destr]
~safe_thread() noexcept;
Effects: If joinable(), calls join(). Otherwise, has no effects.
[Note: Because ~safe_thread is required to be noexcept,
if join() throws then std::terminate() will be called.
--end note]
safe_thread assignment [thread.safe_thread.assign]
safe_thread& operator=(safe_thread&& x) noexcept;
Effects: If joinable(), calls join(). Then, assigns
the state of x to *this and sets x to a default constructed state.
[Note: If join() throws then std::terminate() will be called.
--end note]
Postconditions: x.get_id() == id() and get_id() returns the value
of x.get_id() prior to the assignment.
Returns: *this
safe_thread& operator=(thread&& x) noexcept;
Effects: If joinable(), calls join(). Then, assigns
the state of x to *this and sets x to a default constructed state.
[Note: If join() throws then std::terminate() will be called.
--end note]
Postconditions: x.get_id() == id() and get_id() returns the value
of x.get_id() prior to the assignment.
Returns: *this
safe_thread members [thread.safe_thread.member]
thread to_thread();
Postconditions: *this is set to a default constructed state. The state
of the returned object is the state *this had prior to calling this function.
Returns: A thread object initialized from *this.
</pre>
</blockquote>
</p>
</body>
</html>