Wait.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. {
  7. pal_printf ("Enter Thread 1\n");
  8. DkThreadDelayExecution(3000);
  9. DkEventSet (event1);
  10. pal_printf ("Leave Thread 1\n");
  11. return 0;
  12. }
  13. int thread_2 (void * args)
  14. {
  15. pal_printf ("Enter Thread 2\n");
  16. DkThreadDelayExecution(5000);
  17. DkEventSet (event2);
  18. pal_printf ("Leave Thread 2\n");
  19. return 0;
  20. }
  21. int main() {
  22. pal_printf ("Enter Main Thread\n");
  23. PAL_HANDLE thd1, thd2;
  24. event1 = DkNotificationEventCreate (0);
  25. event2 = DkNotificationEventCreate (0);
  26. thd1 = DkThreadCreate(&thread_1, 0);
  27. if (thd1 == NULL) {
  28. pal_printf("DkThreadCreate failed\n");
  29. return -1;
  30. }
  31. thd2 = DkThreadCreate(&thread_2, 0);
  32. if (thd2 == NULL) {
  33. pal_printf("DkThreadCreate failed\n");
  34. return -1;
  35. }
  36. PAL_HANDLE array[2];
  37. array[0] = event1;
  38. array[1] = event2;
  39. PAL_HANDLE hdl = DkObjectsWaitAny (2, array, NO_TIMEOUT);
  40. pal_printf("event%d is set\n", hdl == event1 ? 1 : 2);
  41. pal_printf("Leave Main Thread\n");
  42. return 0;
  43. }