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