1. header file and compile options
- Header file: #include <pthread.h>
- Compile options: -lpthread
2. Thread the correlation function
- 创建线程
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void (start_rtn)(void *), void *restrict arg); - Thread the initiative to withdraw
void pthread_exit (void * rval_ptr); - Wait for the end of the thread
void pthread_join (pthread_t thread, void ** rval_ptr); - Separate thread, the thread after separation, can not wait pthread_join thread to terminate the thread resources when the thread ends, will be recycled.
int pthread_detach (pthread_t tid); - The thread end of the request, the request only, the thread does not wait for termination of
int pthread_cancel (pthread_t tid); - Determining whether the thread id equal
int pthread_equal (pthread_t tid1, pthread_t tid2 ); - Id get its own thread
pthread_t pthread_self (void); - Thread cleanup function is called when the thread exits, you can register multiple execution sequence in reverse order with the registration
void pthread_cleanup_push (void ( RTN) void ( ), void * Arg);
void pthread_cleanup_pop (int the Execute);
3. Thread Synchronization correlation function
- Mutex (mutex), is essentially a lock to protect the resources are accessed serially.
动态初始化mutex
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutextattr_t *restrict attr);销毁mutex
int pthread_mutex_destroy(pthread_mutex_t *mutex);Interlock amount of lock, the lock is released, prhtead_mutex_trylock regardless of whether the lock is successful, return immediately.
the pthread_mutex_lock int (* pthread_mutex_t the mutex);
int prhtead_mutex_trylock (* pthread_mutex_t the mutex);
int pthread_mutex_unlock (* pthread_mutex_t the mutex);With a mutex lock timeout
int pthread_mutex_timedlock (pthread_mutex_t * restrict mutex, const struct timespec * restrict tsptr);
If a thread attempts to lock a mutex twice, then he will be caught in a deadlock.
- Lock write (reader-writer lock), with respect to the mutex lock write allow more parallelism, very much larger than the number of read write for the scene number.
Read-write locks initialization and destruction
int to pthread_rwlock_init (* pthread_rwlock_t the restrict rwlock is acquired, the restrict const pthread_rwlockattr_t * attr);
int pthread_rwlock_destroy (pthread_rwlock_t * rwlock is acquired);加锁解锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);非阻塞加锁
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);带有超时的读写锁
int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict tsptr);
int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict tsptr);
- Condition variable
Condition variable initialization and destruction
int pthread_cond_init (pthread_cond_t the restrict * cond, const * pthread_condattr_t the restrict attr);
int pthread_cond_destroy (pthread_cond_t * cond);等待条件变量
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict tsptr);Wake threads waiting on the condition variable
int pthread_cond_signal (pthread_cond_t * cond); // wake up at least one thread waiting on the condition
int pthread_cond_broadcast (pthread_cond_t * cond); // wake up all threads waiting on the condition
- Spinlocks
Spinlock initialization and destruction
int pthread_spin_init (pthread_spinlock_t * Lock, pshared int);
int pthread_spin_destroy (pthread_spinlock_t * Lock);自旋锁加锁和解锁
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);
int pthread_spin_unlock(pthread_spinlock_t *lock);
- Barrier (barrier)
barrier is a user synchronization mechanism to coordinate multiple threads operating in parallel, allowing each thread to wait until all the threads are cooperative to a point, and then continue from that point. Pthread_join function as a kind of barrier.
屏障初始化和销毁
int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restrict attr, unsigned int(count);
int pthread_barrier_destroy(pthread_barrier_t *barrier);等待
int pthread_barrier_wait(pthread_barrier_t *barrier);
Reference: "UNIX Advanced Programming Environment"
Reproduced in: https: //www.jianshu.com/p/1f608e1e5f7c