多线程编译编译需要添加-lpthread
//线程的创建
int pthread_create(pthread_t *tid, const pthread_arrt_t* attr,
void*(*start_routine)(void *), void* arg);
//等待线程的结束,非分离的线程在结束后只有执行join才会释放全部资源
int pthread_join(pthread_t thread, void **retval);
//线程离开时的返回值(必须为malloc或者全局变量)
void pthread_exit(void * retval);
//分离线程,被分离的线程在结束后自动释放所有资源
int pthread_detach(pthread_t tid);
//多线程的交互--互斥量和自旋锁
//互斥量
//互斥量申请
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a);
//互斥量加锁
int pthread_mutex_lock(pthread_mutex_t *m);
//互斥量释放解锁
int pthread_mutex_unlock(pthread_mutex_t *m);
//互斥量尝试加锁,非堵塞
int pthread_mutex_trylock(pthread_mutex_t *m);
//互斥量释放
int pthread_mutex_destroy(pthread_mutex_t *m);
//自旋锁
pthread_spinlock_t m_spinlock;
//自旋锁申请
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
//自旋锁加锁
int pthread_spin_lock(pthread_spinlock_t *m_spinlock);
//自旋锁释放
int pthread_spin_unlock(pthread_spinlock_t *m_spinlock);
//自旋锁尝试加锁,非堵塞
int pthread_spin_trylock(pthread_spinlock_t *m_spinlock);
//自旋锁释放
int pthread_spin_destroy(pthread_spinlock_t *m_spinlock);