1#include "../ThreadLocal.h"
2#include "../Thread.h"
3
4#include <boost/noncopyable.hpp>
5#include <stdio.h>
6
7class Test : boost::noncopyable
8{
9 public:
10  Test()
11  {
12    printf("tid=%d, constructing %p\n", muduo::CurrentThread::tid(), this);
13  }
14
15  ~Test()
16  {
17    printf("tid=%d, destructing %p %s\n", muduo::CurrentThread::tid(), this, name_.c_str());
18  }
19
20  const std::string& name() const { return name_; }
21  void setName(const std::string& n) { name_ = n; }
22
23 private:
24  std::string name_;
25};
26
27muduo::ThreadLocal<Test> testObj1;
28muduo::ThreadLocal<Test> testObj2;
29
30void print()
31{
32  printf("tid=%d, obj1 %p name=%s\n",
33         muduo::CurrentThread::tid(),
34	 &testObj1.value(),
35         testObj1.value().name().c_str());
36  printf("tid=%d, obj2 %p name=%s\n",
37         muduo::CurrentThread::tid(),
38	 &testObj2.value(),
39         testObj2.value().name().c_str());
40}
41
42void threadFunc()
43{
44  print();
45  testObj1.value().setName("changed 1");
46  testObj2.value().setName("changed 42");
47  print();
48}
49
50int main()
51{
52  testObj1.value().setName("main one");
53  print();
54  muduo::Thread t1(threadFunc);
55  t1.start();
56  t1.join();
57  testObj2.value().setName("main two");
58  print();
59
60  pthread_exit(0);
61}
62