Advertisement
Guest User

Untitled

a guest
May 26th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. /*
  2. * build command: gcc -pthread -o test test.c; ./test
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <pthread.h>
  9.  
  10. #define COUNT (10)
  11. #define ASCII_LOW_EN (0x61)
  12.  
  13. static bool _Global_Func1_Done = false;
  14. static void _Func1_Print_Num(void)
  15. {
  16. int Idx = 0;
  17.  
  18. /* Print number 1. */
  19. for (Idx = 0; Idx < COUNT; Idx++)
  20. {
  21. printf("%d\n", Idx + 1);
  22. }
  23.  
  24. /* Set done flag. (or send event to func2) */
  25. _Global_Func1_Done = true;
  26. }
  27.  
  28. static bool _Global_Func2_Done = false;
  29. static void _Func2_Print_Char(void)
  30. {
  31. int Idx = 0;
  32.  
  33. /* Wait func1 done. (or recv event from func1) */
  34. while (_Global_Func1_Done == false)
  35. {
  36. sleep(1);
  37. }
  38.  
  39. /* Print char 'a'. */
  40. for (Idx = 0; Idx < COUNT; Idx++)
  41. {
  42. printf("%c\n", ASCII_LOW_EN + Idx);
  43. }
  44.  
  45. _Global_Func2_Done = true;
  46. }
  47.  
  48. static void _Create_Task(void * pFunc)
  49. {
  50. int Ret = 0;
  51. pthread_t stThread = 0;
  52.  
  53. Ret = pthread_create(&stThread, NULL, pFunc, NULL);
  54.  
  55. if (Ret != 0)
  56. {
  57. printf("pthread_create() fail: return = %d\n", Ret);
  58. }
  59. }
  60.  
  61. int main(void)
  62. {
  63. _Create_Task(_Func2_Print_Char);
  64. _Create_Task(_Func1_Print_Num);
  65.  
  66. /* Wait task done. */
  67. while ((_Global_Func1_Done == false) ||
  68. (_Global_Func2_Done == false))
  69. {
  70. sleep(1);
  71. }
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement