Thread.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <pthread.h>
  25. #include "Types.h"
  26. namespace crown
  27. {
  28. typedef void* (*ThreadFunction)(void*);
  29. class Thread
  30. {
  31. public:
  32. /// Constructs the thread and gives it a @a name.
  33. /// @note
  34. /// The actual OS thread creation and execution is
  35. /// deferred to the first call to Thread::start().
  36. Thread(const char* name);
  37. /// Does not stop the thread. The user must call
  38. /// Thread::stop() to effectively stop the thread.
  39. virtual ~Thread();
  40. /// Returns the name of the thread.
  41. const char* name() const;
  42. void join();
  43. void detach();
  44. /// Returns whether the thread is currently running.
  45. bool is_running() const;
  46. /// Returns whether the thread is being asked to stop running.
  47. /// @note
  48. /// The implementer tipically polls this function to
  49. /// determine whether to stop the execution or not.
  50. bool is_terminating() const;
  51. /// Starts the execution of the thread.
  52. /// The function creates the OS thread and starts
  53. /// its execution.
  54. void start();
  55. /// Stops the execution of the thread if it is running.
  56. /// The function releases the OS thread causing its
  57. /// termination.
  58. void stop();
  59. /// Executes in background when the thead is running.
  60. /// The thread has to be started with Thread::start()
  61. virtual int32_t run();
  62. private:
  63. static void* background_proc(void* thiz);
  64. private:
  65. const char* m_name;
  66. bool m_is_running;
  67. bool m_is_terminating;
  68. pthread_t m_thread;
  69. private:
  70. // Disable copying
  71. Thread(const Thread&);
  72. Thread& operator=(const Thread&);
  73. };
  74. } // namespace crown