BoostThreads.hh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * BoostThreads.hh
  3. *
  4. * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2002, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_THREADING_BOOSTTHREADS_HH
  10. #define _LOG4CPP_THREADING_BOOSTTHREADS_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <boost/thread/thread.hpp>
  13. #include <boost/thread/mutex.hpp>
  14. #include <boost/thread/tss.hpp>
  15. #include <cstdio>
  16. #include <string>
  17. LOG4CPP_NS_BEGIN
  18. namespace threading {
  19. static std::string getThreadId() {
  20. char buffer[14];
  21. // Boost.Threads stores the thread ID but doesn't expose it
  22. sprintf(buffer, "not available");
  23. return std::string(buffer);
  24. };
  25. typedef boost::mutex Mutex;
  26. typedef boost::mutex::scoped_lock ScopedLock;
  27. template<typename T> class ThreadLocalDataHolder {
  28. public:
  29. inline T* get() const {
  30. return _localData.get();
  31. };
  32. inline T* operator->() const { return _localData.get(); };
  33. inline T& operator*() const { return *_localData.get(); };
  34. inline T* release() {
  35. return _localData.release();
  36. };
  37. inline void reset(T* p = NULL) {
  38. _localData.reset(p);
  39. };
  40. private:
  41. boost::thread_specific_ptr<T> _localData;
  42. };
  43. }
  44. LOG4CPP_NS_END
  45. #endif