summaryrefslogtreecommitdiff
path: root/libs/thread/doc/scoped_thread.qbk
blob: f98f525003b5350f017ddd979ee372d1725122ef (plain)
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
[/
  (C) Copyright 2008-9 Anthony Williams.
  (C) Copyright 12 Vicente J. Botet Escriba.
  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE_1_0.txt or copy at
  http://www.boost.org/LICENSE_1_0.txt).
]

[section:ScopedThreads Scoped Threads]

[heading Synopsis]

  //#include <boost/thread/scoped_thread.hpp>

  struct detach;
  struct join_if_joinable;
  struct interrupt_and_join_if_joinable;
  template <class CallableThread = join_if_joinable>
  class strict_scoped_thread;
  template <class CallableThread = join_if_joinable>
  class scoped_thread;
  void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;

[section:motovation Motivation]
Based on the scoped_thread class defined in C++ Concurrency in Action Boost.Thread defines a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, call a specific action given as template parameter.

While the scoped_thread class defined in C++ Concurrency in Action is closer to strict_scoped_thread class that doesn't allows any change in the wrapped thread, Boost.Thread provides a class scoped_thread that provides the same non-deprecated interface than __thread.

[endsect]

[section:tutorial Tutorial]

Scoped Threads are wrappers around a thread that allows the user to state what to do at destruction time. One of the common uses is to join the thread at destruction time so this is the default behavior. This is the single difference respect to a thread. While thread call std::terminate() on the destructor is the thread is joinable, strict_scoped_thread<> or scoped_thread<> join the thread if joinable.

The difference between strict_scoped_thread and scoped_thread is that the strict_scoped_thread hides completely the owned thread and so the user can do nothing with the owned thread other than the specific action given as parameter, while scoped_thread provide the same interface than __thread and forwards all the operations.   

  boost::strict_scoped_thread<> t1((boost::thread(f)));
  //t1.detach(); // compile fails
  boost::scoped_thread<> t2((boost::thread(f)));
  t2.detach();

[endsect]

[section:thread_functors Free Thread Functors]

  //#include <boost/thread/scoped_thread.hpp>

  struct detach;
  struct join_if_joinable;
  struct interrupt_and_join_if_joinable;


[section:detach Functor `detach`]

  struct detach
  {
    void operator()(thread& t)
    {
      t.detach();
    }
  };
[endsect]
[section:join_if_joinable Functor `join_if_joinable`]

  struct join_if_joinable
  {
    void operator()(thread& t)
    {
      if (t.joinable())
      {
        t.join();
      }
    }
  };

[endsect]

[section:interrupt_and_join_if_joinable Functor `interrupt_and_join_if_joinable`]

  struct interrupt_and_join_if_joinable
  {
    void operator()(thread& t)
    {
      t.interrupt();
      if (t.joinable())
      {
        t.join();
      }
    }
  };
[endsect]

[endsect]

[section:strict_scoped_thread Class `strict_scoped_thread`]

  // #include <boost/thread/scoped_thread.hpp>

  template <class CallableThread = join_if_joinable>
  class strict_scoped_thread
  {
    thread t_; // for exposition purposes only
  public:

    strict_scoped_thread(strict_scoped_thread const&) = delete;
    strict_scoped_thread& operator=(strict_scoped_thread const&) = delete;

    explicit strict_scoped_thread(thread&& t) noexcept;
    template <typename F&&, typename ...Args>
    explicit strict_scoped_thread(F&&, Args&&...);

    ~strict_scoped_thread();

  };


RAI __thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.

CallableThread: A callable `void(thread&)`.

The default is a `join_if_joinable`.


`std/boost::thread` destructor terminates the program if the __thread is not joinable.
This wrapper can be used to join the thread before destroying it seems a natural need.

[heading Example]

  boost::strict_scoped_thread<> t((boost::thread(F)));

[section:default_constructor Constructor from a __thread]

    explicit strict_scoped_thread(thread&& t) noexcept;

[variablelist

[[Effects:] [move the thread to own `t_`]]

[[Throws:] [Nothing]]

]

[endsect]


[section:call_constructor Move Constructor from a Callable]

        template <typename F&&, typename ...Args>
        explicit strict_scoped_thread(F&&, Args&&...);

[variablelist

[[Effects:] [Construct a internal thread in place.]]

[[Postconditions:] [`*this.t_` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]

[[Throws:] [Any exception the thread construction can throw.]]

]

[endsect]

[section:destructor Destructor]

    ~strict_scoped_thread();

[variablelist

[[Effects:] [Equivalent to `CallableThread()(t_)`.  ]]

[[Throws:] [Nothing: The `CallableThread()(t_)` should not throw when joining the thread as the scoped variable is on a scope outside the thread function.]]

]

[endsect]

[endsect]

[section:scoped_thread Class `scoped_thread`]

    #include <boost/thread/scoped_thread.hpp>

    template <class CallableThread>
    class scoped_thread
    {
      thread t_; // for exposition purposes only
    public:
        scoped_thread() noexcept;
        scoped_thread(const scoped_thread&) = delete;
        scoped_thread& operator=(const scoped_thread&) = delete;

        explicit scoped_thread(thread&& th) noexcept;
        template <typename F&&, typename ...Args>
        explicit scoped_thread(F&&, Args&&...);

        ~scoped_thread();

        // move support
        scoped_thread(scoped_thread && x) noexcept;
        scoped_thread& operator=(scoped_thread && x) noexcept;

        void swap(scoped_thread& x) noexcept;

        typedef thread::id id;

        id get_id() const noexcept;

        bool joinable() const noexcept;
        void join();
    #ifdef BOOST_THREAD_USES_CHRONO
        template <class Rep, class Period>
        bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
        template <class Clock, class Duration>
        bool try_join_until(const chrono::time_point<Clock, Duration>& t);
    #endif

        void detach();

        static unsigned hardware_concurrency() noexcept;
        static unsigned physical_concurrency() noexcept;

        typedef thread::native_handle_type native_handle_type;
        native_handle_type native_handle();

    #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
        void interrupt();
        bool interruption_requested() const noexcept;
    #endif


    };

    void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;


RAI __thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.

CallableThread: A callable void(thread&).
The default is join_if_joinable.

thread std::thread destructor terminates the program if the thread is not joinable.
Having a wrapper that can join the thread before destroying it seems a natural need.

Remark: `scoped_thread` is not a __thread as __thread is not designed to be derived from as a polymorphic type.

Anyway `scoped_thread` can be used in most of the contexts a __thread could be used as it has the
same non-deprecated interface with the exception of the construction.

[heading Example]

  boost::scoped_thread<> t((boost::thread(F)));
  t.interrupt();


[section:default_constructor Default Constructor]

    scoped_thread() noexcept;

[variablelist

[[Effects:] [Constructs a scoped_thread instance that wraps to __not_a_thread__.]]

[[Postconditions:] [`this->get_id()==thread::id()`]]

[[Throws:] [Nothing]]

]

[endsect]

[section:move_constructor Move Constructor]

    scoped_thread(scoped_thread&& other) noexcept;

[variablelist

[[Effects:] [Transfers ownership of the scoped_thread managed by `other` (if any) to the newly constructed scoped_thread instance.]]

[[Postconditions:] [`other.get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the construction]]

[[Throws:] [Nothing]]

]

[endsect]

[section:move_assignment Move assignment operator]

    scoped_thread& operator=(scoped_thread&& other) noexcept;

[variablelist

[[Effects:] [Transfers ownership of the scoped_thread managed by `other` (if
any) to `*this`. 

- if defined `BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE`: If there was a `scoped_thread` previously associated with `*this` then that `scoped_thread` is detached, DEPRECATED

- if defined `BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE`: If the `scoped_thread` is joinable calls to std::terminate.
]]

[[Postconditions:] [`other->get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the assignment.]]

[[Throws:] [Nothing]]

]

[endsect]

[section:thread_constructor Move Constructor from a __thread]

    scoped_thread(thread&& t);

[variablelist

[[Effects:] [move the thread to own `t_`.]]

[[Postconditions:] [`*this.t_` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]

[[Throws:] [Nothing]]

]

[endsect]

[section:call_constructor Move Constructor from a Callable]

        template <typename F&&, typename ...Args>
        explicit scoped_thread(F&&, Args&&...);

[variablelist

[[Effects:] [Construct a internal thread in place.]]

[[Postconditions:] [`*this.t_` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]

[[Throws:] [Any exception the thread construction can throw.]]

]

[endsect]


[section:destructor Destructor]

    ~scoped_thread();

[variablelist

[[Effects:] [Equivalent to `CallableThread()(t_)`.  ]]

[[Throws:] [Nothing: The `CallableThread()(t_)` should not throw when joining the thread as the scoped variable is on a scope outside the thread function.]]

]

[endsect]


[section:joinable Member function `joinable()`]

    bool joinable() const noexcept;

[variablelist

[[Returns:] [Equivalent to return t_.joinable().]]

[[Throws:] [Nothing]]

]


[endsect]

[section:join Member function `join()`]

    void join();

[variablelist

[[Effects:] [Equivalent to t_.join().]]

]

[endsect]

[section:try_join_for Member function `try_join_for()`]

        template <class Rep, class Period>
        bool try_join_for(const chrono::duration<Rep, Period>& rel_time);

[variablelist

[[Effects:] [Equivalent to return `t_.try_join_for(rel_time)`.]]

]

[endsect]

[section:try_join_until Member function `try_join_until()`]

        template <class Clock, class Duration>
        bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time);

[variablelist

[[Effects:] [Equivalent to return `t_.try_join_until(abs_time)`.]]

]

[endsect]



[section:detach Member function `detach()`]

    void detach();

[variablelist

[[Effects:] [Equivalent to `t_.detach()`.]]

]

[endsect]


[section:get_id Member function `get_id()`]

    thread::id get_id() const noexcept;

[variablelist

[[Effects:] [Equivalent to return `t_.get_id()`.]]

]

[endsect]

[section:interrupt Member function `interrupt()`]

    void interrupt();

[variablelist

[[Effects:] [Equivalent to `t_.interrupt()`.]]

]
        

[endsect]

[section:hardware_concurrency Static member function `hardware_concurrency()`]

    unsigned hardware_concurrency() noexecpt;

[variablelist

[[Effects:] [Equivalent to return `thread::hardware_concurrency()`.]]

]

[endsect]


[section:physical_concurrency Static member function `physical_concurrency()`]

    unsigned physical_concurrency() noexecpt;

[variablelist

[[Effects:] [Equivalent to return `thread::physical_concurrency()`.]]

]

[endsect]


[section:nativehandle Member function `native_handle()`]

    typedef thread::native_handle_type native_handle_type;
    native_handle_type native_handle();

[variablelist

[[Effects:] [Equivalent to return `t_.native_handle()`.]]

]

[endsect]

[section:swap Member function `swap()`]

    void swap(scoped_thread& other) noexcept;

[variablelist

[[Effects:] [Equivalent `t_.swap(other.t_)`.]]

]

[endsect]



[endsect]
[section:non_member_swap Non-member function `swap(scoped_thread&,scoped_thread&)`]

    #include <boost/thread/scoped_thread.hpp>

    void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;

[variablelist

[[Effects:] [`lhs.swap(rhs)`.]]

]

[endsect]
[endsect]