1. Thread Creation
-
if man pthread_create does not work:
sudo apt install -y manpages-posix manpages-posix-dev
pthread_create(), pthread_join()
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); int pthread_join(pthread_t thread, void **retval);
threads-api/thread_create.c1 #include <assert.h> 2 #include <stdio.h> 3 #include <pthread.h> 4 5 typedef struct { 6 int a; 7 int b; 8 } myarg_t; 9 10 void *mythread(void *arg) { 11 myarg_t *args = (myarg_t *) arg; 12 printf("%d %d\n", args->a, args->b); 13 return NULL; 14 } 15 16 int main(int argc, char *argv[]) { 17 pthread_t p; 18 myarg_t args = { 10, 20 }; 19 20 int rc = pthread_create(&p, NULL, mythread, &args); 21 assert(rc == 0); 22 (void) pthread_join(p, NULL); 23 printf("done\n"); 24 return 0; 25 }
$ gcc -Wall -o thread_create thread_create.c -pthread $ ./thread_create 10 20 done $