#include "../Singleton.h" #include "../ThreadLocal.h" #include "../Thread.h" #include #include #include class Test : boost::noncopyable { public: Test() { printf("tid=%d, constructing %p\n", muduo::CurrentThread::tid(), this); } ~Test() { printf("tid=%d, destructing %p %s\n", muduo::CurrentThread::tid(), this, name_.c_str()); } const std::string& name() const { return name_; } void setName(const std::string& n) { name_ = n; } private: std::string name_; }; #define STL muduo::Singleton >::instance().value() void print() { printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &STL, STL.name().c_str()); } void threadFunc(const char* changeTo) { print(); STL.setName(changeTo); sleep(1); print(); } int main() { STL.setName("main one"); muduo::Thread t1(boost::bind(threadFunc, "thread1")); muduo::Thread t2(boost::bind(threadFunc, "thread2")); t1.start(); t2.start(); t1.join(); print(); t2.join(); pthread_exit(0); }