主要函数说明
- int pthread_mutex_init (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr)创建一个锁;
- int pthread_mutex_destroy (pthread_mutex_t *__mutex)销毁锁;
- int pthread_mutex_trylock (pthread_mutex_t *__mutex)尝试去lock,lock不到时立即返回错误;
- int pthread_mutex_lock (pthread_mutex_t *__mutex)尝试去lock,lock不到时永久等待;
- int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex, const struct timespec *__restrict __abstime)尝试去lock,lock不到时等待__abstime的时间;
- int pthread_mutex_unlock (pthread_mutex_t *__mutex)释放一个锁;
- int pthread_mutexattr_init (pthread_mutexattr_t *__attr)初始化锁参数;
- int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)销毁锁参数
- int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,int __pshared)设定锁的进程共享属性;
- int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)设定锁的加锁类型;
- int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,int __protocol)设定锁对线程优先级的影响策略;
互斥锁使用示例
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#define DBG_PRINT(fmt, args...) {printf("%s %d ", __FUNCTION__, __LINE__);printf(fmt,##args);}
/**
* [msDelay_select 用select()实现的ms级别线程休眠]
* @param msTime [休眠线程msTime时间,单位毫秒]
*/
void msDelay_select(unsigned msTime)
{
struct timeval time;
if(msTime == 0)
{
DBG_PRINT("delay time