summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/thread/example/synchronized_person.cpp
blob: 7930c0b00e3f7c63a1244e1276c44a44a431b26b (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
// (C) Copyright 2012 Vicente Botet
//
//  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)

#define BOOST_THREAD_VERSION 4

#include <iostream>
#include <string>
#include <boost/thread/synchronized_value.hpp>

//class SafePerson {
//public:
//  std::string GetName() const {
//    const_unique_access<std::string> name(nameGuard);
//    return *name;
//  }
//  void SetName(const std::string& newName)  {
//    unique_access<std::string> name(nameGuard);
//    *name = newName;
//  }
//private:
//  unique_access_guard<std::string> nameGuard;
//};

class SafePerson {
public:
  std::string GetName() const {
    return *name;
  }
  void SetName(const std::string& newName)  {
    *name = newName;
  }

private:
  boost::synchronized_value<std::string> name;
};

class Person {
public:
  std::string GetName() const {
    return name;
  }
  void SetName(const std::string& newName) {
    name = newName;
  }
private:
  std::string name;
};
typedef boost::synchronized_value<Person> Person_ts;


//class SafeMemberPerson {
//public:
//  SafeMemberPerson(unsigned int age) :
//    memberGuard(age)
//  {  }
//  std::string GetName() const  {
//    const_unique_access<Member> member(memberGuard);
//    return member->name;
//  }
//  void SetName(const std::string& newName)  {
//    unique_access<Member> member(memberGuard);
//    member->name = newName;
//  }
//private:
//  struct Member
//  {
//    Member(unsigned int age) :
//      age(age)
//    {    }
//    std::string name;
//    unsigned int age;
//  };
//  unique_access_guard<Member> memberGuard;
//};

class SafeMemberPerson {
public:
  SafeMemberPerson(unsigned int age) :
    member(Member(age))
  {  }
  std::string GetName() const  {
    return member->name;
  }
  void SetName(const std::string& newName)  {
    member->name = newName;
  }
private:
  struct Member  {
    Member(unsigned int age) :
      age(age)
    {    }
    std::string name;
    unsigned int age;
  };
  boost::synchronized_value<Member> member;
};


class Person2 {
public:
  Person2(unsigned int age) : age_(age)
  {}
  std::string GetName() const  {
    return name_;
  }
  void SetName(const std::string& newName)  {
    name_ = newName;
  }
  unsigned int GetAge() const  {
    return age_;
  }

private:
  std::string name_;
  unsigned int age_;
};
typedef boost::synchronized_value<Person2> Person2_ts;

//===================

//class HelperPerson {
//public:
//  HelperPerson(unsigned int age) :
//    memberGuard(age)
//  {  }
//  std::string GetName() const  {
//    const_unique_access<Member> member(memberGuard);
//    Invariant(member);
//    return member->name;
//  }
//  void SetName(const std::string& newName)  {
//    unique_access<Member> member(memberGuard);
//    Invariant(member);
//    member->name = newName;
//  }
//private:
//  void Invariant(const_unique_access<Member>& member) const  {
//    if (member->age < 0) throw std::runtime_error("Age cannot be negative");
//  }
//  struct Member  {
//    Member(unsigned int age) :
//      age(age)
//    {    }
//    std::string name;
//    unsigned int age;
//  };
//  unique_access_guard<Member> memberGuard;
//};

class HelperPerson {
public:
  HelperPerson(unsigned int age) :
    member(age)
  {  }
  std::string GetName() const  {
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
        auto memberSync = member.synchronize();
#else
    boost::const_strict_lock_ptr<Member>  memberSync = member.synchronize();
#endif
    Invariant(memberSync);
    return memberSync->name;
  }
  void SetName(const std::string& newName)  {
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
    auto memberSync = member.synchronize();
#else
    boost::strict_lock_ptr<Member> memberSync = member.synchronize();
#endif
    Invariant(memberSync);
    memberSync->name = newName;
  }
private:
  struct Member  {
    Member(unsigned int age) :
      age(age)
    {    }
    std::string name;
    unsigned int age;
  };
  void Invariant(boost::const_strict_lock_ptr<Member> & mbr) const
  {
    if (mbr->age < 1) throw std::runtime_error("Age cannot be negative");
  }
  boost::synchronized_value<Member> member;
};

class Person3 {
public:
  Person3(unsigned int age) :
    age_(age)
  {  }
  std::string GetName() const  {
    Invariant();
    return name_;
  }
  void SetName(const std::string& newName)  {
    Invariant();
    name_ = newName;
  }
private:
  std::string name_;
  unsigned int age_;
  void Invariant() const  {
    if (age_ < 1) throw std::runtime_error("Age cannot be negative");
  }
};

typedef boost::synchronized_value<Person3> Person3_ts;

int main()
{
  {
    SafePerson p;
    p.SetName("Vicente");
  }
  {
    Person_ts p;
    p->SetName("Vicente");
  }
  {
    SafeMemberPerson p(1);
    p.SetName("Vicente");
  }
  {
    Person2_ts p(1);
    p->SetName("Vicente");
  }
  {
    HelperPerson p(1);
    p.SetName("Vicente");
  }
  {
    Person3_ts p(1);
    p->SetName("Vicente");
  }
  {
    Person3_ts p1(1);
    Person3_ts p2(2);
    Person3_ts p3(3);
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
    auto  lk1 = p1.unique_synchronize(boost::defer_lock);
    auto  lk2 = p2.unique_synchronize(boost::defer_lock);
    auto  lk3 = p3.unique_synchronize(boost::defer_lock);
#else
    boost::unique_lock_ptr<Person3>  lk1 = p1.unique_synchronize(boost::defer_lock);
    boost::unique_lock_ptr<Person3>  lk2 = p2.unique_synchronize(boost::defer_lock);
    boost::unique_lock_ptr<Person3>  lk3 = p3.unique_synchronize(boost::defer_lock);
#endif
    boost::lock(lk1,lk2,lk3);

    lk1->SetName("Carmen");
    lk2->SetName("Javier");
    lk3->SetName("Matias");
  }
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS \
&& ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
  {
    Person3_ts p1(1);
    Person3_ts p2(2);
    Person3_ts p3(3);

    auto t  = boost::synchronize(p1,p2,p3);
    std::get<0>(t)->SetName("Carmen");
    std::get<1>(t)->SetName("Javier");
    std::get<2>(t)->SetName("Matias");
  }
  {
    const Person3_ts p1(1);
    Person3_ts p2(2);
    const Person3_ts p3(3);

    auto t  = boost::synchronize(p1,p2,p3);
    //std::get<0>(t)->SetName("Carmen");
    std::get<1>(t)->SetName("Javier");
    //std::get<2>(t)->SetName("Matias");
  }
#endif
  return 0;
}