Wait.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* This Hello World demostrate a simple multithread program */
  4. #include "pal.h"
  5. #include "pal_debug.h"
  6. PAL_HANDLE event1, event2;
  7. int thread_1 (void * args)
  8. {
  9. pal_printf ("Enter Thread 1\n");
  10. DkThreadDelayExecution(3000);
  11. DkEventSet (event1);
  12. pal_printf ("Leave Thread 1\n");
  13. return 0;
  14. }
  15. int thread_2 (void * args)
  16. {
  17. pal_printf ("Enter Thread 2\n");
  18. DkThreadDelayExecution(5000);
  19. DkEventSet (event2);
  20. pal_printf ("Leave Thread 2\n");
  21. return 0;
  22. }
  23. int main() {
  24. pal_printf ("Enter Main Thread\n");
  25. PAL_HANDLE thd1, thd2;
  26. event1 = DkNotificationEventCreate (0);
  27. event2 = DkNotificationEventCreate (0);
  28. thd1 = DkThreadCreate(&thread_1, 0, 0);
  29. if (thd1 == NULL) {
  30. pal_printf("DkThreadCreate failed\n");
  31. return -1;
  32. }
  33. thd2 = DkThreadCreate(&thread_2, 0, 0);
  34. if (thd2 == NULL) {
  35. pal_printf("DkThreadCreate failed\n");
  36. return -1;
  37. }
  38. PAL_HANDLE array[2];
  39. array[0] = event1;
  40. array[1] = event2;
  41. PAL_HANDLE hdl = DkObjectsWaitAny (2, array, NO_TIMEOUT);
  42. pal_printf("event%d is set\n", hdl == event1 ? 1 : 2);
  43. pal_printf("Leave Main Thread\n");
  44. return 0;
  45. }