1
0

ex1.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* Creates two threads, one printing 10000 "a"s, the other printing
  10. 10000 "b"s.
  11. Illustrates: thread creation, thread joining. */
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include "pthread.h"
  16. static void *process(void * arg)
  17. {
  18. int i;
  19. printf("Starting process %s\n", (char *)arg);
  20. for (i = 0; i < 10000; i++)
  21. write(1, (char *) arg, 1);
  22. return NULL;
  23. }
  24. #define sucfail(r) (r != 0 ? "failed" : "succeeded")
  25. int libc_ex1(void)
  26. {
  27. int pret, ret = 0;
  28. pthread_t th_a, th_b;
  29. void *retval;
  30. ret += (pret = pthread_create(&th_a, NULL, process, (void *)"a"));
  31. printf("create a %s %d\n", sucfail(pret), pret);
  32. ret += (pret = pthread_create(&th_b, NULL, process, (void *)"b"));
  33. printf("create b %s %d\n", sucfail(pret), pret);
  34. ret += (pret = pthread_join(th_a, &retval));
  35. printf("join a %s %d\n", sucfail(pret), pret);
  36. ret += (pret = pthread_join(th_b, &retval));
  37. printf("join b %s %d\n", sucfail(pret), pret);
  38. return ret;
  39. }
  40. #include <finsh.h>
  41. FINSH_FUNCTION_EXPORT(libc_ex1, example 1 for libc);