Thread.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016/10/1 Bernard The first version
  9. */
  10. #pragma once
  11. #include <stdint.h>
  12. #include <rtthread.h>
  13. namespace rtthread
  14. {
  15. /** The Thread class allow defining, creating, and controlling thread functions in the system. */
  16. class Thread
  17. {
  18. public:
  19. typedef void (*thread_func_t)(void *param);
  20. /** Allocate a new thread without starting execution
  21. @param priority initial priority of the thread function. (default: osPriorityNormal).
  22. @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
  23. @param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
  24. */
  25. Thread(rt_uint32_t stack_size = 2048,
  26. rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3,
  27. rt_uint32_t tick = 20,
  28. const char *name = "th");
  29. Thread(void (*entry)(void *p),
  30. void *p = RT_NULL,
  31. rt_uint32_t stack_size = 2048,
  32. rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3,
  33. rt_uint32_t tick = 20,
  34. const char *name = "th");
  35. virtual ~Thread();
  36. bool start();
  37. static void sleep(int32_t millisec);
  38. void wait(int32_t millisec);
  39. void join(int32_t millisec = -1);
  40. protected:
  41. virtual void run();
  42. private:
  43. static void func(Thread *pThis);
  44. private:
  45. rt_thread_t _thread;
  46. thread_func_t _entry;
  47. void *_param;
  48. /* event for thread join */
  49. struct rt_event _event;
  50. bool started;
  51. };
  52. }