ex1.c 950 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* Creates two threads, one printing 10000 "a"s, the other printing
  2. 10000 "b"s.
  3. Illustrates: thread creation, thread joining. */
  4. #include <stddef.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include "pthread.h"
  8. static void *process(void * arg)
  9. {
  10. int i;
  11. printf("Starting process %s\n", (char *)arg);
  12. for (i = 0; i < 10000; i++)
  13. write(1, (char *) arg, 1);
  14. return NULL;
  15. }
  16. #define sucfail(r) (r != 0 ? "failed" : "succeeded")
  17. int main(void)
  18. {
  19. int pret, ret = 0;
  20. pthread_t th_a, th_b;
  21. void *retval;
  22. ret += (pret = pthread_create(&th_a, NULL, process, (void *)"a"));
  23. printf("create a %s %d\n", sucfail(pret), pret);
  24. ret += (pret = pthread_create(&th_b, NULL, process, (void *)"b"));
  25. printf("create b %s %d\n", sucfail(pret), pret);
  26. ret += (pret = pthread_join(th_a, &retval));
  27. printf("join a %s %d\n", sucfail(pret), pret);
  28. ret += (pret = pthread_join(th_b, &retval));
  29. printf("join b %s %d\n", sucfail(pret), pret);
  30. return ret;
  31. }