Thread.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * File : Thread.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2016, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016/10/1 Bernard The first version
  23. */
  24. #pragma once
  25. #include <stdint.h>
  26. #include <rtthread.h>
  27. namespace rtthread
  28. {
  29. /** The Thread class allow defining, creating, and controlling thread functions in the system. */
  30. class Thread
  31. {
  32. public:
  33. typedef void (*thread_func_t) (void *param);
  34. /** Allocate a new thread without starting execution
  35. @param priority initial priority of the thread function. (default: osPriorityNormal).
  36. @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
  37. @param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
  38. */
  39. Thread(rt_uint32_t stack_size = 2048,
  40. rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2)/3,
  41. rt_uint32_t tick = 20,
  42. const char *name = "th");
  43. Thread(void (*entry)(void *p),
  44. void *p = RT_NULL,
  45. rt_uint32_t stack_size = 2048,
  46. rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2)/3,
  47. rt_uint32_t tick = 20,
  48. const char *name = "th");
  49. virtual ~Thread();
  50. bool start();
  51. static void sleep(int32_t millisec);
  52. void wait(int32_t millisec);
  53. void join(int32_t millisec = -1);
  54. protected:
  55. virtual void run();
  56. private:
  57. static void func(Thread *pThis);
  58. private:
  59. rt_thread_t _thread;
  60. thread_func_t _entry;
  61. void *_param;
  62. /* event for thread join */
  63. struct rt_event _event;
  64. bool started;
  65. };
  66. }