-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjthread.html
More file actions
480 lines (408 loc) · 30.4 KB
/
jthread.html
File metadata and controls
480 lines (408 loc) · 30.4 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<!doctype html>
<html class="cpprefjp" lang="ja" itemscope="" itemtype="http://schema.org/WebPage">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NXNBNVBTJS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-NXNBNVBTJS');
</script>
<meta charset="UTF-8">
<title>jthread - cpprefjp C++日本語リファレンス</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="keywords" content="
C++,標準ライブラリ,リファレンス,ドキュメント,STL,std,thread,std,class,cpp20
">
<meta name="title" content="jthread - cpprefjp C++日本語リファレンス" />
<meta itemprop="name" content="jthread - cpprefjp C++日本語リファレンス" />
<meta property="og:title" content="jthread - cpprefjp C++日本語リファレンス" />
<meta property="og:url" content="https://cpprefjp.github.io/reference/thread/jthread.html" />
<meta property="og:site_name" content="cpprefjp - C++日本語リファレンス" />
<meta property="og:type" content="article" />
<meta property="og:description" content="クラス`jthread`は、[`thread`](thread.md)と同じく、新しい実行のスレッド(thread of execution)(以下、単にスレッドとする)の作成/待機/その他操作を行う機構を提供する。このクラスはさらに、実行しているスレッドに対する停止要求を扱う機能や、自動で[`join`](jthread/join.md)操作を行う機能を提供する。" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="jthread - cpprefjp C++日本語リファレンス" />
<meta name="twitter:url" content="https://cpprefjp.github.io/reference/thread/jthread.html" />
<meta name="twitter:description" content="クラス`jthread`は、[`thread`](thread.md)と同じく、新しい実行のスレッド(thread of execution)(以下、単にスレッドとする)の作成/待機/その他操作を行う機構を提供する。このクラスはさらに、実行しているスレッドに対する停止要求を扱う機能や、自動で[`join`](jthread/join.md)操作を行う機能を提供する。" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="https://cpprefjp.github.io/rss.xml" />
<link rel="apple-touch-icon" sizes="180x180" href="../../static/favicons/apple-touch-icon.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="icon" type="image/png" sizes="32x32" href="../../static/favicons/favicon-32x32.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="icon" type="image/png" sizes="16x16" href="../../static/favicons/favicon-16x16.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="manifest" href="../../manifest.json?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<meta name="theme-color" content="#f5f8fc">
<link rel="stylesheet" href="../../static/pygments/default.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<!-- <link rel="stylesheet" href="../../static/css/root.css"> -->
<link href="../../static/kunai/css/kunai-stage-0.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../static/kunai/css/kunai-stage-1.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../static/kunai/css/kunai-stage-2.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../static/kunai/css/kunai-stage-3.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<script type="text/javascript" src="../../static/kunai/js/kunai-vendor.js?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95"></script>
<script type="text/javascript" src="../../static/kunai/js/kunai.js?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var kn = new Kunai;
kn.cpprefjp();
});
</script>
</head>
<body>
<header data-kunai-mdinfo="{"meta": {"header": ["thread"], "namespace": ["std"], "id-type": ["class"], "cpp": ["cpp20"]}, "sources": [{"id": "43a5a652396ace34e48c43593ed73504d07d3eb0", "source": "#include <iostream>\n#include <cstdint>\n#include <thread>\n\nstd::uint64_t sum1 = 0;\nstd::uint64_t sum2 = 0;\n\nvoid f1(std::stop_token stoken, std::uint64_t n)\n{\n sum1 = 0;\n for (std::uint64_t i = 1; i < n; ++i) {\n if (stoken.stop_requested()) {\n // \u4e2d\u65ad\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u304d\u305f\u306e\u3067\u30b9\u30ec\u30c3\u30c9\u3092\u7d42\u4e86\u3059\u308b\n break;\n }\n sum1 += i;\n }\n}\n\nvoid f2(std::uint64_t n)\n{\n sum2 = 0;\n for (std::uint64_t i = 1; i < n; ++i) {\n sum2 += i;\n }\n}\n\nint main()\n{\n {\n // \u95a2\u6570\u306e\u7b2c1\u5f15\u6570\u304cstd::stop_token\u578b\u3067\u3042\u308b\u5834\u5408\u3001\n // \u30b9\u30ec\u30c3\u30c9\u306b\u4e2d\u65ad\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308c\u308b\u3088\u3046\u306b\u306a\u308b\n std::jthread jt1 {f1, 1'000'000};\n std::this_thread::sleep_for(std::chrono::milliseconds{3});\n jt1.request_stop(); // \u30b9\u30ec\u30c3\u30c9\u306e\u4e2d\u65ad\u8981\u6c42\u3092\u767a\u884c\n\n // \u30b9\u30ec\u30c3\u30c9\u5b9f\u884c\u3059\u308b\u95a2\u6570\u304cstd::stop_token\u3092\u53d7\u3051\u53d6\u3089\u306a\u3044\u5834\u5408\u3001\n // \u4e2d\u65ad\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f7f\u7528\u305b\u305a\u3001\n // \u30c7\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u81ea\u52d5\u7684\u306bjoin\u3059\u308b\u30b9\u30ec\u30c3\u30c9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\n std::jthread jt2 {\n [] { f2(1'000'000); }\n };\n } // jthread\u306e\u30c7\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u306f\u3001\u4e2d\u65ad\u8981\u6c42\u3092\u767a\u884c\u3057\u3001\u30b9\u30ec\u30c3\u30c9\u306e\u7d42\u4e86\u3092\u5f85\u6a5f\u3059\u308b\n\n std::cout << sum1 << std::endl; // \u8a08\u7b97\u3067\u304d\u305f\u3068\u3053\u308d\u307e\u3067\u8868\u793a\n std::cout << sum2 << std::endl;\n}\n"}, {"id": "3b55186ed35c96795234b410da781588e3ecdafc", "source": "#include <cassert>\n#include <thread>\n#include <stop_token>\n\nint main()\n{\n int x = 0, y = 0;\n\n std::jthread jt([&](std::stop_token st) { x++; });\n\n // \u30b9\u30ec\u30c3\u30c9\u306b\u5bfe\u3059\u308b\u505c\u6b62\u8981\u6c42\u306e\u4f5c\u6210\u306b\u5408\u308f\u305b\u3066\u547c\u3073\u51fa\u3055\u308c\u308b\n // \u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9a\u7fa9\u3059\u308b\u3002\n std::stop_callback sc { jt.get_stop_token(), [&] { y++; }};\n\n assert(y == 0);\n\n // \u660e\u793a\u7684\u306bjoin()\u3092\u547c\u3073\u51fa\u3055\u305a\u306bt\u3092\u4e0a\u66f8\u304d\u3059\u308b\u3002\n // \u3053\u306e\u3068\u304d\u3001\u30e0\u30fc\u30d6\u4ee3\u5165\u6f14\u7b97\u5b50\u306e\u547c\u3073\u51fa\u3057\u306e\u4e2d\u3067\u3001\n // \u81ea\u52d5\u3067\u505c\u6b62\u8981\u6c42\u306e\u4f5c\u6210\u3068join()\u306e\u547c\u3073\u51fa\u3057\u304c\u884c\u308f\u308c\u308b\u3002\n jt = std::jthread{};\n\n assert(x == 1 && y == 1);\n\n return 0;\n}\n"}], "page_id": ["reference", "thread", "jthread"]}">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../../index.html">
<div class="title-wrapper clearfix">
<div class="title">cpprefjp - C++日本語リファレンス</div>
</div>
</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<div class="google-search">
<script>
(function() {
var cx = '013316413321391058734:ji_u66hl7hq';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<div class="gcse-search"></div>
</div>
</li>
<li>
<a href="https://github.com/cpprefjp/site">GitHub Project</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main id="main" role="main">
<div class="container-fluid">
<div class="row">
<div class="col-sm-9 col-sm-push-3" itemscope itemtype="http://schema.org/Article">
<div class="row">
<div class="col-sm-12 google-search-result">
<gcse:searchresults></gcse:searchresults>
</div>
</div>
<div class="row">
<div class="col-sm-12 content-header">
<ol class="breadcrumb">
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../index.html" itemprop="url">
<i class="fa fa-fw fa-home"></i>
</a>
</span>
</li>
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../reference.html" itemprop="url">
<span itemprop="name">リファレンス</span>
</a>
</span>
</li>
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../reference/thread.html" itemprop="url">
<span itemprop="name">thread</span>
</a>
</span>
</li>
<li class="active" itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<span itemprop="name">jthread</span>
</span>
</li>
</ol>
<div class="crsearch"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12 edit-button">
<p class="text-right"><small>
最終更新日時(UTC):
<span itemprop="datePublished" content="2026-03-23T12:31:23">
2026年03月23日 12時31分23秒
</span>
<br/>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span itemprop="name">Raclamusi</span>
</span>
が更新
</small></p>
<p class="text-right">
<a class="history" target="_blank" href="https://github.com/cpprefjp/site/commits/master/reference/thread/jthread.md">
<span class="fa fa-fw fa-clock-o fa-flip-horizontal"></span>履歴
</a>
<a class="edit" target="_blank" href="https://github.com/cpprefjp/site/edit/master/reference/thread/jthread.md">
<span class="fa fa-fw fa-pencil"></span>編集
</a>
</p>
</div>
</div>
<div class="row">
<div class="col-sm-12 content-body">
<div class="identifier-type">class</div><div class="header"><thread></div><h1 itemprop="name"><span class="namespace" title="namespace std">std::</span><span class="token">jthread</span><span class="cpp cpp20" title="C++20で追加">(C++20)</span></h1>
<div itemprop="articleBody"><p><div class="codehilite"><pre><span></span><code><span class="k">namespace</span><span class="w"> </span><span class="nn">std</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">jthread</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
</p>
<h2>概要</h2>
<p>クラス<code>jthread</code>は、<code><a href="thread.html">thread</a></code>と同じく、新しい実行のスレッド(thread of execution)(以下、単にスレッドとする)の作成/待機/その他操作を行う機構を提供する。このクラスはさらに、実行しているスレッドに対する停止要求を扱う機能や、自動で<code><a href="jthread/join.html">join</a></code>操作を行う機能を提供する。</p>
<h2><code>thread</code>クラスとの違い</h2>
<ul>
<li>停止要求のサポート: <code>jthread</code>は自身と関連付けられたスレッドに対する停止要求を扱う仕組みを提供する。このために<code><a href="../stop_token.html"><stop_token></a></code>ヘッダに定義されたクラスを利用する。</li>
<li>自動join機能: <code>jthread</code>はデストラクタやムーブ代入演算子が呼び出されたとき、もし自身に関連付けられたスレッドが存在する場合は、スレッドの停止要求を作成し、その後<code><a href="jthread/join.html">join()</a></code>を呼び出してスレッドの終了を待機する。そのため<code>thread</code>クラスと異なり、<code><a href="jthread/joinable.html">joinable()</a> == true</code> であってもデストラクタやムーブ代入演算子を呼び出し可能であり、<code><a href="../exception/terminate.html">std::terminate()</a></code>は呼び出されない。</li>
</ul>
<h2>メンバ関数</h2>
<table border="1" bordercolor="#888" style="border-collapse:collapse">
<thead>
<tr>
<th>名前</th>
<th>説明</th>
<th>対応バージョン</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="jthread/op_constructor.html">(constructor)</a></code></td>
<td>コンストラクタ</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/op_destructor.html">(destructor)</a></code></td>
<td>デストラクタ</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/op_assign.html">operator=</a></code></td>
<td>代入演算子</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/swap.html">swap</a></code></td>
<td>別の<code>jthread</code>と交換する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/joinable.html">joinable</a></code></td>
<td>スレッドに関連付けられているか否かを取得する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/join.html">join</a></code></td>
<td>スレッドが終了するまで待機する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/detach.html">detach</a></code></td>
<td>スレッドの管理を手放す</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/get_id.html">get_id</a></code></td>
<td>関連付けられているスレッドのスレッド識別子を取得する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/native_handle.html">native_handle</a></code></td>
<td>スレッドに関連付けられたネイティブハンドルを取得する[<a class="cpprefjp-defined-word" data-desc="処理系定義の動作。処理系によって事前に定めた動作をする" href="../../implementation-compliance.html#dfn-implementation-defined-behavior">処理系定義</a>]</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/get_stop_source.html">get_stop_source</a></code></td>
<td>停止要求を作成するための<code><a href="../stop_token/stop_source.html">stop_source</a></code>オブジェクトを取得する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/get_stop_token.html">get_stop_token</a></code></td>
<td>停止状態を問い合わせるための<code><a href="../stop_token/stop_token.html">stop_token</a></code>オブジェクトを取得する</td>
<td>C++20</td>
</tr>
<tr>
<td><code><a href="jthread/request_stop.html">request_stop</a></code></td>
<td>スレッドに対する停止要求を作成する</td>
<td>C++20</td>
</tr>
</tbody>
</table>
<h2>静的メンバ関数</h2>
<table border="1" bordercolor="#888" style="border-collapse:collapse">
<thead>
<tr>
<th>名前</th>
<th>説明</th>
<th>対応バージョン</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="jthread/hardware_concurrency.html">hardware_concurrency</a></code></td>
<td>処理系によりサポートされるスレッド並行数を取得する</td>
<td>C++20</td>
</tr>
</tbody>
</table>
<h2>メンバ型</h2>
<table border="1" bordercolor="#888" style="border-collapse:collapse">
<thead>
<tr>
<th>名前</th>
<th>説明</th>
<th>対応バージョン</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="jthread/id.html">id</a></code></td>
<td>スレッド識別子 (type-alias)</td>
<td>C++20</td>
</tr>
<tr>
<td><code>native_handle_type</code></td>
<td>ネイティブハンドル型 (type-alias)[<a class="cpprefjp-defined-word" data-desc="処理系定義の動作。処理系によって事前に定めた動作をする" href="../../implementation-compliance.html#dfn-implementation-defined-behavior">処理系定義</a>]</td>
<td>C++20</td>
</tr>
</tbody>
</table>
<h2>非メンバ関数</h2>
<table border="1" bordercolor="#888" style="border-collapse:collapse">
<thead>
<tr>
<th>名前</th>
<th>説明</th>
<th>対応バージョン</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="jthread/swap_free.html">swap</a></code></td>
<td>2つの<code>jthread</code>オブジェクトを入れ替える</td>
<td>C++20</td>
</tr>
</tbody>
</table>
<h2>備考</h2>
<ul>
<li>型<code>native_handle_type</code>およびメンバ関数<code>native_handle</code>について、同メンバの存在有無およびその意味は<a class="cpprefjp-defined-word" data-desc="処理系定義の動作。処理系によって事前に定めた動作をする" href="../../implementation-compliance.html#dfn-implementation-defined-behavior">処理系定義</a>となる。</li>
<li>型<code>id</code>および型<code>native_handle_type</code>は、<code>thread</code>クラスで定義しているものと同じものを使用する。</li>
</ul>
<h2>例</h2>
<h3>基本的な使い方</h3>
<p><div class="yata" id="43a5a652396ace34e48c43593ed73504d07d3eb0"><div class="codehilite"><pre><span></span><code><span class="cp">#include <a href="../iostream.html"><iostream></a></span>
<span class="cp">#include <a href="../cstdint.html"><cstdint></a></span>
<span class="cp">#include <a href="../thread.html"><thread></a></span>
<span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">sum1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">sum2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">f1</span><span class="p">(</span><span class="n"><a href="../stop_token/stop_token.html">std::stop_token</a></span><span class="w"> </span><span class="n">stoken</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">sum1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n"><a href="../stop_token/stop_token/stop_requested.html">stoken.stop_requested()</a></span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// 中断リクエストがきたのでスレッドを終了する</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">sum1</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">f2</span><span class="p">(</span><span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">sum2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n"><a href="../cstdint/uint64_t.html">std::uint64_t</a></span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">sum2</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// 関数の第1引数が<a href="../stop_token/stop_token.html">std::stop_token</a>型である場合、</span>
<span class="w"> </span><span class="c1">// スレッドに中断リクエストを送れるようになる</span>
<span class="w"> </span><span class="n"><a href="">std::jthread</a></span><span class="w"> </span><span class="n">jt1</span><span class="w"> </span><span class="p">{</span><span class="n">f1</span><span class="p">,</span><span class="w"> </span><span class="mi">1'000'000</span><span class="p">};</span>
<span class="w"> </span><span class="n"><a href="this_thread/sleep_for.html">std::this_thread::sleep_for</a></span><span class="p">(</span><span class="n"><a href="../chrono/duration_aliases.html">std::chrono::milliseconds</a></span><span class="p">{</span><span class="mi">3</span><span class="p">});</span>
<span class="w"> </span><span class="n"><a href="jthread/request_stop.html">jt1.request_stop()</a></span><span class="p">;</span><span class="w"> </span><span class="c1">// スレッドの中断要求を発行</span>
<span class="w"> </span><span class="c1">// スレッド実行する関数が<a href="../stop_token/stop_token.html">std::stop_token</a>を受け取らない場合、</span>
<span class="w"> </span><span class="c1">// 中断リクエストを使用せず、</span>
<span class="w"> </span><span class="c1">// デストラクタで自動的にjoinするスレッドオブジェクトとして使用する</span>
<span class="w"> </span><span class="n"><a href="">std::jthread</a></span><span class="w"> </span><span class="n">jt2</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="p">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">f2</span><span class="p">(</span><span class="mi">1'000'000</span><span class="p">);</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// jthreadのデストラクタでは、中断要求を発行し、スレッドの終了を待機する</span>
<span class="w"> </span><span class="n"><a href="../iostream/cout.html">std::cout</a></span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">sum1</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n"><a href="../ostream/endl.html">std::endl</a></span><span class="p">;</span><span class="w"> </span><span class="c1">// 計算できたところまで表示</span>
<span class="w"> </span><span class="n"><a href="../iostream/cout.html">std::cout</a></span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">sum2</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n"><a href="../ostream/endl.html">std::endl</a></span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
</div></p>
<h4>出力例</h4>
<p><pre><code>48458670270
499999500000
</code></pre></p>
<h3>stop_callbackと組み合わせる例</h3>
<p><div class="yata" id="3b55186ed35c96795234b410da781588e3ecdafc"><div class="codehilite"><pre><span></span><code><span class="cp">#include <a href="../cassert.html"><cassert></a></span>
<span class="cp">#include <a href="../thread.html"><thread></a></span>
<span class="cp">#include <a href="../stop_token.html"><stop_token></a></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="n"><span style="color:#ff0000">std::jthread</span></span><span class="w"> </span><span class="n">jt</span><span class="p">([</span><span class="o">&</span><span class="p">](</span><span class="n"><a href="../stop_token/stop_token.html">std::stop_token</a></span><span class="w"> </span><span class="n">st</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="p">});</span>
<span class="w"> </span><span class="c1">// スレッドに対する停止要求の作成に合わせて呼び出される</span>
<span class="w"> </span><span class="c1">// コールバックを定義する。</span>
<span class="w"> </span><span class="n"><a href="../stop_token/stop_callback.html">std::stop_callback</a></span><span class="w"> </span><span class="n">sc</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">jt</span><span class="p">.</span><span class="n"><a href="jthread/get_stop_token.html">get_stop_token()</a></span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">y</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="p">}};</span>
<span class="w"> </span><span class="n"><a href="../cassert/assert.html">assert</a></span><span class="p">(</span><span class="n">y</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// 明示的にjoin()を呼び出さずにtを上書きする。</span>
<span class="w"> </span><span class="c1">// このとき、ムーブ代入演算子の呼び出しの中で、</span>
<span class="w"> </span><span class="c1">// 自動で停止要求の作成とjoin()の呼び出しが行われる。</span>
<span class="w"> </span><span class="n">jt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n"><span style="color:#ff0000">std::jthread</span></span><span class="p">{};</span>
<span class="w"> </span><span class="n"><a href="../cassert/assert.html">assert</a></span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
</div></p>
<h4>出力</h4>
<p><pre><code></code></pre></p>
<h2>バージョン</h2>
<h3>言語</h3>
<ul>
<li>C++20</li>
</ul>
<h3>処理系</h3>
<ul>
<li><a href="../../implementation.html#clang">Clang</a>:</li>
<li><a href="../../implementation.html#gcc">GCC</a>: 10.2.0 <span aria-label="検証済" role="img" title="検証済">✅</span></li>
<li><a href="../../implementation.html#visual_cpp">Visual C++</a>: ??</li>
</ul></div>
</div>
</div>
</div>
<div id="sidebar" class="col-sm-3 col-sm-pull-9">
</div>
</div>
</div>
</main>
<footer class="footer navbar navbar-default">
<div class="container-fluid">
<p><small>
本サイトの情報は、
<a href="https://creativecommons.org/licenses/by/4.0/deed.ja" rel="nofollow">クリエイティブ・コモンズ 表示 4.0 非移植 ライセンス(CC BY)</a>
の下に提供されています。
</small></p>
</div>
</footer>
</body>
</html>