Singleton_test.cc revision 2d3b4de6
1cd139dc7SShuo Chen#include "../Singleton.h"
2cd139dc7SShuo Chen#include "../Thread.h"
3cd139dc7SShuo Chen
4cd139dc7SShuo Chen#include <boost/noncopyable.hpp>
5cd139dc7SShuo Chen#include <stdio.h>
62d3b4de6SShuo Chen#include <unistd.h>
7cd139dc7SShuo Chen
8cd139dc7SShuo Chenclass Test : boost::noncopyable
9cd139dc7SShuo Chen{
10cd139dc7SShuo Chen public:
11cd139dc7SShuo Chen  Test()
12cd139dc7SShuo Chen  {
13cd139dc7SShuo Chen    printf("tid=%d, constructing %p\n", muduo::CurrentThread::tid(), this);
14cd139dc7SShuo Chen  }
15cd139dc7SShuo Chen
16cd139dc7SShuo Chen  ~Test()
17cd139dc7SShuo Chen  {
18cd139dc7SShuo Chen    printf("tid=%d, destructing %p %s\n", muduo::CurrentThread::tid(), this, name_.c_str());
19cd139dc7SShuo Chen  }
20cd139dc7SShuo Chen
21cd139dc7SShuo Chen  const std::string& name() const { return name_; }
22cd139dc7SShuo Chen  void setName(const std::string& n) { name_ = n; }
23cd139dc7SShuo Chen
24cd139dc7SShuo Chen private:
25cd139dc7SShuo Chen  std::string name_;
26cd139dc7SShuo Chen};
27cd139dc7SShuo Chen
28cd139dc7SShuo Chenvoid threadFunc()
29cd139dc7SShuo Chen{
30cd139dc7SShuo Chen  printf("tid=%d, %p name=%s\n",
31cd139dc7SShuo Chen         muduo::CurrentThread::tid(),
32cd139dc7SShuo Chen         &muduo::Singleton<Test>::instance(),
33cd139dc7SShuo Chen         muduo::Singleton<Test>::instance().name().c_str());
34cd139dc7SShuo Chen  muduo::Singleton<Test>::instance().setName("only one, changed");
35cd139dc7SShuo Chen}
36cd139dc7SShuo Chen
378ce0c3b5SShuo Chentemplate<unsigned N>
388ce0c3b5SShuo Chenclass Destruct
398ce0c3b5SShuo Chen{
408ce0c3b5SShuo Chen public:
418ce0c3b5SShuo Chen  Destruct()
428ce0c3b5SShuo Chen  {
438ce0c3b5SShuo Chen    printf("construct %d\n", N);
448ce0c3b5SShuo Chen    more();
458ce0c3b5SShuo Chen  }
468ce0c3b5SShuo Chen
478ce0c3b5SShuo Chen  ~Destruct()
488ce0c3b5SShuo Chen  {
498ce0c3b5SShuo Chen    printf("destruct %d\n", N);
508ce0c3b5SShuo Chen  }
518ce0c3b5SShuo Chen
528ce0c3b5SShuo Chen  void more()
538ce0c3b5SShuo Chen  {
548ce0c3b5SShuo Chen    muduo::Singleton<Destruct<N-1> >::instance();
558ce0c3b5SShuo Chen  }
568ce0c3b5SShuo Chen};
578ce0c3b5SShuo Chen
588ce0c3b5SShuo Chentemplate<>
598ce0c3b5SShuo Chenvoid Destruct<0>::more()
608ce0c3b5SShuo Chen{
618ce0c3b5SShuo Chen}
628ce0c3b5SShuo Chen
63cd139dc7SShuo Chenint main()
64cd139dc7SShuo Chen{
65cd139dc7SShuo Chen  muduo::Singleton<Test>::instance().setName("only one");
66cd139dc7SShuo Chen  muduo::Thread t1(threadFunc);
67cd139dc7SShuo Chen  t1.start();
68cd139dc7SShuo Chen  t1.join();
69cd139dc7SShuo Chen  printf("tid=%d, %p name=%s\n",
70cd139dc7SShuo Chen         muduo::CurrentThread::tid(),
71cd139dc7SShuo Chen         &muduo::Singleton<Test>::instance(),
72cd139dc7SShuo Chen         muduo::Singleton<Test>::instance().name().c_str());
738ce0c3b5SShuo Chen
742d3b4de6SShuo Chen  muduo::Singleton<Destruct<100> >::instance();
752d3b4de6SShuo Chen  printf("ATEXIT_MAX = %ld\n", sysconf(_SC_ATEXIT_MAX));
76cd139dc7SShuo Chen}
77