]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/doc/sync_tutorial.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / thread / doc / sync_tutorial.qbk
CommitLineData
7c673cae
FG
1[/
2 (C) Copyright 2012 Vicente J. Botet Escriba.
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
6]
7
8[section:tutorial Tutorial]
9
10
11[@http://web.archive.org/web/20140531071228/http://home.roadrunner.com/~hinnant/mutexes/locking.html Handling mutexes in C++] is an excellent tutorial. You need just replace std and ting by boost.
12
13
14[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2406.html Mutex, Lock, Condition Variable Rationale] adds rationale for the design decisions made for mutexes, locks and condition variables.
15
16
17In addition to the C++11 standard locks, Boost.Thread provides other locks and some utilities that help the user to make their code thread-safe.
18
19[include internal_locking.qbk]
20
21[include external_locking.qbk]
22
23[section:with Executing Around a Function]
24
25In particular, the library provides a way to lock around the execution of a function.
26
27 template <class Lockable, class Function, class... Args>
28 auto with_lock_guard(
29 Lockable& m,
30 Function&& func,
31 Args&&... args
32 ) -> decltype(func(boost::forward<Args>(args)...)) {
33 boost::lock_guard<Lockable> lock(m);
34 return func(boost::forward<Args>(args)...);
35 }
36
37that can be used with regular functions:
38
39 int func(int, int&);
40 //...
41 boost::mutex m;
42 int a;
43 int result = boost::with_lock_guard(m, func, 1, boost::ref(a));
44
45with boost::bind:
46
47 int result = boost::with_lock_guard(
48 m, boost::bind(func, 2, boost::ref(a))
49 );
50
51or with lambda expression:
52
53 int a;
54 int result = boost::with_lock_guard(
55 m,
56 [&a](int x) {
57 // this scope is protected by mutex m
58 a = 3;
59 return x + 4;
60 },
61 5
62 );
63
64[endsect] [/ With]
65
66[endsect] [/ Tutorial]