PThread 学习笔记
线程相关函数:
1. pthread 相关函数的头文件:
#include <pthread.h>
2. pthread 的创建函数:
int pthread_create (pthread_t *thread_id, const pthread_attr_t *attributes, void *(*thread_function)(void *), void *arguments);
3. pthread 对应的结束函数:
int pthread_exit (void *status);
4. 一个线程可以等待另一个线程的结束:
int pthread_join (pthread_t thread, void **status_ptr);
5. 获取当前线程的 id:
pthread_t pthread_self ();
6. 比较线程 id:
int pthread (pthread_t t1, pthread_t t2);
互斥量有两个基本操作:锁与解锁。有五个基本函数用来管理互斥量:
1. int pthread_mutex_init (pthread_mutex_t *mut, const pthread_mutexattr_t *attr);
2. int pthread_mutex_lock (pthread_mutex_t *mut);
3. int pthread_mutex_unlock (pthread_mutex_t *mut);
4. int pthread_mutex_trylock (pthread_mutex_t *mut);
5. int pthread_mutex_destroy (pthread_mutex_t *mut);
条件变量
1. int pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *attr);
2. int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mut);
3. int pthread_cond_signal (pthread_cond_t *cond);
4. int pthread_cond_broadcast (pthread_cond_t *cond);
5. int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mut, const struct timespec *abstime);
6. int pthread_cond_destroy (pthread_cond_t *cond);