Wait.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* This Hello World demostrate a simple multithread program */
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. PAL_HANDLE event1, event2;
  5. int thread_1(void* args) {
  6. pal_printf("Enter Thread 1\n");
  7. DkThreadDelayExecution(3000);
  8. DkEventSet(event1);
  9. pal_printf("Leave Thread 1\n");
  10. return 0;
  11. }
  12. int thread_2(void* args) {
  13. pal_printf("Enter Thread 2\n");
  14. DkThreadDelayExecution(5000);
  15. DkEventSet(event2);
  16. pal_printf("Leave Thread 2\n");
  17. return 0;
  18. }
  19. int main() {
  20. pal_printf("Enter Main Thread\n");
  21. PAL_HANDLE thd1, thd2;
  22. event1 = DkNotificationEventCreate(0);
  23. event2 = DkNotificationEventCreate(0);
  24. thd1 = DkThreadCreate(&thread_1, 0);
  25. if (thd1 == NULL) {
  26. pal_printf("DkThreadCreate failed\n");
  27. return -1;
  28. }
  29. thd2 = DkThreadCreate(&thread_2, 0);
  30. if (thd2 == NULL) {
  31. pal_printf("DkThreadCreate failed\n");
  32. return -1;
  33. }
  34. PAL_HANDLE array[2];
  35. array[0] = event1;
  36. array[1] = event2;
  37. PAL_HANDLE hdl = DkObjectsWaitAny(2, array, NO_TIMEOUT);
  38. pal_printf("event%d is set\n", hdl == event1 ? 1 : 2);
  39. pal_printf("Leave Main Thread\n");
  40. return 0;
  41. }