多线程互斥锁使用递归属性可能的场景

有必要使用递归互斥量的一种情况。这里 有一个超时函数,它允许安排另一个函数在未来的某个时间运行。假设线程并不是很昂贵当资源 就可以为每个挂起的函数创建一个线程。线程在时间未到时将一直等待,时间到了再调用请求的函数

  • 如果我们不能创建线程,或者安排函数运行时间已过,这时候问题就出现了,在这些情况下,我们只需要在当前上下文中调用之前请求运行的函数。
  • 因为函数要获取的锁和我们现在占用的是同一把锁,所以除非函数是递归的,否则就会出现死锁。
    makethread 函数分离状态创建线程。传给timeout的func函数在未来执行,不希望一直空等线程结束,可以调用sleep等待超时到期如果希望等待当时间不使整数秒 需要使用 nanosleep 或者 clock_nanosleep 函数,提供更高精度的休眠时间。
  • timeout的调用着需要占有互斥量来检测条件,并且把retry函数安排为原子操作。retry函数试图对同一个互斥量进行加锁。除非互斥
    量是递归属性的,否则,如果timeout 函数直接调用retry,会导致死锁。
#include <apue.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

extern int makethread(void *(*)(void *),void *);

struct to_info{
	void (*to_fn)(void *);  /*function*/
	void *to_arg;           /*argument*/
	struct timespec to_wait;  /*time to wait*/
};

#define SECTONSEC  1000000000   /* seconds to nanoseconds */

#if !defined(CLOCK_REALTIME) || defined(BSD)
#define clock_nanosleep(ID,FL,REQ,REM)  nanosleep((REQ),(REM))
#endif

#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 0
#define USECTONSEC 1000   /* microseconds to nanoseconds */

void clock_gettime(int id,struct timespec *tsp)
{
	struct timevel tv;
	gettimeofday(&tv,NULL);
	tsp->tv_sec = tv.tv_sec;
	tsp->tv_nsec = tv.tv_usec * USECTONSEC;
}
#endif   // CLOCK_REALTIME


void * timeout_helper(void *arg)
{
	struct to_info *tip;
	tip = (struct to_info *)arg;
	clock_nanosleep(CLOCK_REALTIME,0,&tip->to_wait,NULL);
	(*tip->to_fn)(tip->to_arg);
	free(arg);
	return(0);
}

void timeout(const struct timespec *when,void (*func)(void *),void *arg)
{
	struct timespec now;
	struct to_info  *tip;
	int 				 err;

	clock_gettime(CLOCK_REALTIME,&now);
	if((when->tv_sec > now.tv_sec) ||
			(when->tv_sec == now.tv_sec && when->tv_nsec > now.tv_nsec)){

		tip = malloc(sizeof(struct to_info));
		if(tip != NULL){
			tip->to_fn = func;
			tip->to_arg = arg;
			tip->to_wait.tv_sec = when->tv_sec - now.tv_sec;
			if(when->tv_nsec >= now.tv_nsec){
				tip->to_wait.tv_nsec = when->tv_nsec - now.tv_nsec;
			}else{
				tip->to_wait.tv_sec--;
				tip->to_wait.tv_nsec = SECTONSEC - now.tv_nsec + when->tv_nsec;
			}

			err = makethread(timeout_helper,(void *)tip);
			if(err == 0)
				return;
			else
				free(tip);
		}
	}

	/**
	 * we get here if (a) when <= now , or
	 * 					(b) malloc fails, or
	 * 					(c) we can't make a thread,
	 * so we just call the function now
	 *
	 */

	(*func)(arg);
}


pthread_mutexattr_t attr;
pthread_mutex_t mutex;

void retry(void *arg)
{
	pthread_mutex_lock(&mutex);

	/*preform retry steps*/

	pthread_mutex_unlock(&mutex);
}

int main(void)
{
	int err,condition,arg;
	struct timespec when;

	if((err = pthread_mutexattr_init(&attr)) != 0)
		err_exit(err,"pthread_mutexattr_init failed");
	if((err = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)) != 0)
		err_exit(err,"can't set recursive type");
	if((err = pthread_mutex_init(&mutex,&attr)) != 0)
		err_exit(err,"can't create recursive mutex");

	/* continue processing */

	pthread_mutex_lock(&mutex);


	/**
	 * check the condition under the protection of a lock to
	 * make the check and the call to timeout atomic
	 */

	if(condition){
		/**
		 * calculate the absolute time when we want to retry
		 */

		clock_gettime(CLOCK_REALTIME,&when);
		when.tv_sec += 10;   /*10 seconds from now*/

		timeout(&when, retry,(void *)((unsigned long)arg));
	}

	pthread_mutex_unlock(&mutex);

	/*continue processing.....*/

	exit(0);
}

代码来源于 《unix环境高级编程》第三版一书
环境安装参考: https://includestdio.com/835.html

添加评论

友情链接:蝴蝶教程