pthread_mutex_timedlock

参考博文:https://blog.csdn.net/wynter_/article/details/53443994

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
 
int main(void) {
	int err;
	struct timespec tout;  //纳秒级别
	struct tm *tmp;
	char buf[64];
	
	pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;  //初始化锁
	
	//1
	pthread_mutex_lock(&lock);
	printf("mutex is locked\n");
	clock_gettime(CLOCK_REALTIME, &tout);
	tmp = localtime(&tout.tv_sec);

	// strftime(char *str, size_t maxsize, const char *fmt, struct tm *time)
	// 按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,
	// 然后存储在字符串str中(至多maxsize 个字符)
	strftime(buf, sizeof(buf), "%r", tmp);
	printf("Current time is %s.\n", buf);
	tout.tv_sec += 10;   //延迟10s

	//2
	err = pthread_mutex_timedlock(&lock, &tout);
	clock_gettime(CLOCK_REALTIME, &tout);
	tmp = localtime(&tout.tv_sec);
	strftime(buf, sizeof(buf), "%r", tmp);
	printf("The time is now %s\n", buf);
	
	if(err == 0) {
		printf("mutex locked again!\n");

	} else {
		printf("Can't lock mutex again: %s\n", strerror(err));
	}

	pthread_mutex_unlock(&lock);
	return 0;
}
/test/test-77$ ./main
mutex is locked
Current time is 01:21:58 PM.
The time is now 01:22:08 PM
Can't lock mutex again: Connection timed out

局部变量 tout 要声明位置,避免在 lock 期间释放
也可以考虑 pthread_mutex_trylock

当程序试图获取一个已加锁的互斥量时,pthread_mutex_timedlock 允许线程在获取不到锁时,阻塞固定时间,然后获取或者返回错误码

pthread_mutex_timedlock 与 pthread_mutex_lock 基本等价
在达到超时时间时,pthread_mutex_timedlock 不会对互斥量进行加锁,而是返回错误码 ETIMEOUT.

#include <pthread.h>
#include <time.h>
int pthread_mutex_timedlock(pthread_mutex_t mutex, const struct timespec *tsptr);
返回值:若成功,返回0;否则,返回错误编号

问题:A线程拿了锁,B线程用 timedlock 去加锁(超时时间10秒),如果A线程在4秒的时候释放锁,那B线程还需要再等待 6 秒吗?
答案是不会的

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

pthread_t  tid;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *release_lock_thread(void *arg) {
    printf("release_lock_thread, enter ...\n");
	sleep(5);
	printf("release_lock_thread, before unlock\n");
	pthread_mutex_unlock(&lock);
	printf("release_lock_thread, after unlock\n");
    printf("release_lock_thread, exit ...\n");
    pthread_exit(NULL);
}

int main(int argc, const char *argv[]) {
	int ret;
	int err;
	struct timespec tout;  //纳秒级别
	struct tm *tmp;
	char buf[64];

	printf("main thread, enter ...\n");

	ret = pthread_create(&tid, NULL, release_lock_thread, NULL);
	if(ret != 0) {
		printf("main thread fail to pthread_create release_lock_thread, %s\n",strerror(errno));
		exit(EXIT_FAILURE);
	}
	printf("main thread, pthread_create release_lock_thread successful\n");

	printf("main thread, before lock\n");
	pthread_mutex_lock(&lock);
	printf("main thread, after lock\n");
	clock_gettime(CLOCK_REALTIME, &tout);
	tmp = localtime(&tout.tv_sec);
	strftime(buf, sizeof(buf), "%r", tmp);
	printf("Current time is %s.\n", buf);
	tout.tv_sec += 10; 

	printf("main thread, before timedlock\n");
	err = pthread_mutex_timedlock(&lock, &tout);
	printf("main thread, after timedlock\n");
	clock_gettime(CLOCK_REALTIME, &tout);
	tmp = localtime(&tout.tv_sec);
	strftime(buf, sizeof(buf), "%r", tmp);
	printf("The time is now %s\n", buf);
	
	if(err == 0) {
		printf("main thread, mutex locked again!\n");
	} else {
		printf("main thread, Can't lock mutex again: %s\n", strerror(err));
	}

	printf("main thread, exit ...\n");
    pthread_join(tid,NULL);
	exit(EXIT_SUCCESS);
}
/test/test-77$ ./main1
main thread, enter ...
main thread, pthread_create release_lock_thread successful
main thread, before lock
main thread, after lock
release_lock_thread, enter ...
Current time is 01:39:09 PM.
main thread, before timedlock


release_lock_thread, before unlock
release_lock_thread, after unlock
release_lock_thread, exit ...
main thread, after timedlock
The time is now 01:39:14 PM
main thread, mutex locked again!
main thread, exit ...

猜你喜欢

转载自blog.csdn.net/wangkai6666/article/details/118544654
今日推荐