#include "../ThreadLocalSingleton.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_; }; void threadFunc(const char* changeTo) { printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &muduo::ThreadLocalSingleton::instance(), muduo::ThreadLocalSingleton::instance().name().c_str()); muduo::ThreadLocalSingleton::instance().setName(changeTo); printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &muduo::ThreadLocalSingleton::instance(), muduo::ThreadLocalSingleton::instance().name().c_str()); // manually delete it muduo::ThreadLocalSingleton::destroy(); } int main() { muduo::ThreadLocalSingleton::instance().setName("main one"); muduo::Thread t1(boost::bind(threadFunc, "thread1")); muduo::Thread t2(boost::bind(threadFunc, "thread2")); t1.start(); t2.start(); t1.join(); printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &muduo::ThreadLocalSingleton::instance(), muduo::ThreadLocalSingleton::instance().name().c_str()); t2.join(); }