1 threads contain thread IDs, a set of register values, stacks, scheduling priorities and policies, signal-masking words, errno variables, and thread-private data. All the information for the process is shared for all threads of the process, including executable text, program global memory and heap memory, stack, and file descriptors.
Thread can get its own thread ID through the Pthread_self function
#include <pthread.h>pthread_t pthread_self (void)
New processes can be created with the Pthread_create function
#include <pthread.h>int pthread_create (pthread_t *restrict TIDP, const pthread _attr_t *Restrict attr, void * (*START_RTN) (voidvoid *restrict Arg)
The memory unit that TIDP points to is set to the thread ID of the newly created thread.
The attr is used to specify the properties of the thread, and now it is temporarily set to null.
The thread runs from the address of the START_RTN function, STRAT_RTN only has an untyped pointer argument arg, and if there are multiple arguments, put them into the struct, passing in ARG
2 Thread termination
#include <pthread.h>void pthread_exit (void *roal_ptr)
Other threads in the same process can be canceled through the Pthread_cancel function
void pthread_cancel (pthread_t tid)
3 Thread Synchronization
Create/Eliminate Mutex amount
int pthread_mutex_init (pthread_mutex_t *restrict Mutex, const pthread_mutexattr_t * restrict attr); int Pthread_mutex_destroy (pthread_mutex_t *<pthread.h>int Pthread_mutex_lock ( pthread_mutex_t *mutex); int pthread_mutex_trylock (pthread_mutex_t *mutex); int pthread_mutex_unlock (pthread_mutex_t *mutex);
UNIX Environment Advanced Programming Learning Note (vii): Threads