std_thread.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // std_thread.cpp
  3. // threadpp
  4. //
  5. // Created by Melo Yao on 1/15/15.
  6. // Copyright (c) 2015 Melo Yao. All rights reserved.
  7. //
  8. #ifndef __threadpp__std_thread__hpp__
  9. #define __threadpp__std_thread__hpp__
  10. #include <functional>
  11. #include <algorithm>
  12. namespace threadpp
  13. {
  14. inline std_thread::id_type std_thread::null_id()
  15. {
  16. return 0;
  17. }
  18. inline void* std_fp_delegate(std_thread::runnable r, void* context)
  19. {
  20. r(context);
  21. return nullptr;
  22. }
  23. inline std_thread::std_thread(runnable r,void* t):_thread(std::bind(std_fp_delegate,r,t))
  24. {
  25. }
  26. inline std_thread::~std_thread()
  27. {
  28. }
  29. inline void std_thread::join()
  30. {
  31. _thread.join();
  32. }
  33. inline void std_thread::detach()
  34. {
  35. _thread.detach();
  36. }
  37. inline bool std_thread::is_equal(const std_thread& t) const
  38. {
  39. return _thread.get_id() == t._thread.get_id();
  40. }
  41. inline std_thread::id_type std_thread::get_id() const
  42. {
  43. return std::hash<std::thread::id>()(_thread.get_id());
  44. }
  45. inline void std_thread::sleep(unsigned long millisecs)
  46. {
  47. std::this_thread::sleep_for(std::chrono::milliseconds(millisecs));
  48. }
  49. inline std_thread::id_type std_thread::current_thread_id()
  50. {
  51. return std::hash<std::thread::id>()(std::this_thread::get_id());
  52. }
  53. }
  54. #endif