thread_test.c 549 B

1234567891011121314151617181920
  1. // /usr/bin/gcc -g -Wall -Wextra -Werror -std=c17 -o thread_test.bin thread_test.c -lpthread
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. void *thread_function(void *_arg)
  6. {
  7. unsigned int *arg = (unsigned int *)_arg;
  8. printf(" new thread: my tid is %u\n", *arg);
  9. return NULL;
  10. }
  11. int main()
  12. {
  13. pthread_t thread;
  14. pthread_create(&thread, NULL, thread_function, &thread); // pass the thread id to the new thread
  15. printf("main thread: my tid is %lu\n", (unsigned long)pthread_self());
  16. usleep(2000);
  17. return 0;
  18. }