yield.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include <stdio.h>
  6. #include "prthread.h"
  7. #include "prinit.h"
  8. #ifndef XP_OS2
  9. #include "private/pprmisc.h"
  10. #include <windows.h>
  11. #else
  12. #include "primpl.h"
  13. #include <os2.h>
  14. #endif
  15. #define THREADS 10
  16. void
  17. threadmain(void *_id)
  18. {
  19. int id = (int)_id;
  20. int index;
  21. printf("thread %d alive\n", id);
  22. for (index=0; index<10; index++) {
  23. printf("thread %d yielding\n", id);
  24. PR_Sleep(0);
  25. printf("thread %d awake\n", id);
  26. }
  27. printf("thread %d dead\n", id);
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. int index;
  32. PRThread *a[THREADS];
  33. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 5);
  34. PR_STDIO_INIT();
  35. for (index=0; index<THREADS; index++) {
  36. a[index] = PR_CreateThread(PR_USER_THREAD,
  37. threadmain,
  38. (void *)index,
  39. PR_PRIORITY_NORMAL,
  40. index%2?PR_LOCAL_THREAD:PR_GLOBAL_THREAD,
  41. PR_JOINABLE_THREAD,
  42. 0);
  43. }
  44. for(index=0; index<THREADS; index++) {
  45. PR_JoinThread(a[index]);
  46. }
  47. printf("main dying\n");
  48. }