#include<pthread.h> // create a thread // thread: // to receive the tid // pthread_t is long int in most OS, but it's structure in some OS // attr: // a structure to determine the attributes for new thread // NULL for using the default attributes // unually NULL is enough // start_routine: // the callback function the thread start in // arg: // the arguments passing to the "start_routine" // return value: // return 0 for success, or errorno on error intpthread_create(pthread_t *thread, constpthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
// to print the error information while failed #include<stdio.h> #include<string.h> printf("%s: %s\n", "pthread_create", strerror(errorno));
#include<pthread.h> // blocked and waiting the termination of the thread // thread: // thread id to join // retval: // receive the return value of the thread // return value: // return 0 for success, or errorno on error intpthread_join(pthread_t thread, void **retval);
#include<pthread.h> // detach the thread and the thread will release the resources automatically // thread: // thread id to detach // return value: // return 0 for success, or errorno on error intpthread_detach(pthread_t thread);
#include<pthread.h> // send a cancellation request to a thread // thread: // thread identifier // return value: // return 0 for success, or errorno on error intpthread_cancel(pthread_t thread);