Linux学习随手笔记-互斥锁

一、为什么要引入互斥量和条件变量
用于线程间同步,来保护数据。
二、锁的创建于销毁
pthread_mutex_t mutex(创建锁,变量名自取)

初始化锁

int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *attr)

参数分析
pthread_mutex_t restrict mutex:创建的锁的地址;
const pthread_mutexattr_t *attr:属性,暂时设定为NULL。
函数返回值
成功返回0,否则返回错误编码

如果锁是动态创建的则需要销毁

int pthread_mutex_destroy(pthread_mutex_t *mutex)

三、加锁解锁

int pthread_mutex_lock(pthread_mutex_t *mutex) 	
int pthread_mutex_trylock(pthread_mutex_t *mutex) 	
int pthread_mutex_unlock(pthread_mutex_t *mutex) 

第一个函数,如果锁还在,则将锁拿过来,如果锁不在,则会阻塞线程直到有锁。
第二个函数与第一个函数不同的是如果无锁不会阻塞而是直接返回

带有超时的锁

int pthread_mutex_timedlock(pthread_mutex_t mutex, const struct timespec *tsptr);

与pthread_mutex_lock基本一样,但这个函数可以设置一个等待超时,超时后它将直接返回错误码ETIMEDOUT。
注:该函数的超时设定是绝对时间。

demo1

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>


pthread_mutex_t lock;
int count;

void* fun1(void *arg)
{
        pthread_mutex_lock(&lock);
        while(1)
        {
                printf("thread1 is running the lock is %d\n",++count);
                if(count == 5)
                {
                        pthread_mutex_unlock(&lock);
                        pthread_exit(NULL);
		}
                sleep(1);
        }
        return (void *)0;
}
void* fun2(void *arg)
{

        while(1)
        {
                printf("thread2 is running the lock is %d\n",++count);
                if(count == 10)
                {
                        pthread_mutex_unlock(&lock);
                        pthread_exit(NULL);
                }
                sleep(1);
        }
        return (void *)0;

}
int main()
{

        pthread_t thread1,thread2;
        int err;
        void *ret;

        err = pthread_mutex_init(&lock,NULL);
        if(err != 0)
        {
                printf("init mutex failed\n");
        }
	    err = pthread_create(&thread1,NULL,fun1,NULL);
        if(err != 0)
        {
                printf("thread1 failed\n");
        }

        err = pthread_create(&thread2,NULL,fun2,NULL);
        if(err != 0)
        {
                printf("thread2 failed\n");
        }

        pthread_join(thread1,&ret);
        pthread_join(thread2,&ret);
        return 0;
}

在这里插入图片描述可以看到thread1先运行五次后退出,thread2继续运行(thread1和thread2的运行先后取决于内核调度的算法)。

demo2

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

pthread_mutex_t mutex;

void* fun1(void *arg)
{
        pthread_mutex_lock(&mutex);
        while(1)
        {
                printf("thread1 is running\n");
                sleep(1);
        }
}

int main()
{
        struct timespec tout;
        struct tm *time;
        pthread_t thread1;
        void *ret;

        pthread_create(&thread1,NULL,fun1,NULL);
        clock_gettime(CLOCK_REALTIME,&tout);
        sleep(1);
        tout.tv_sec += 5;
        pthread_mutex_timedlock(&mutex,&tout);

        time = localtime(&tout.tv_sec);
        printf("main thread is running\n");
        printf("now time%s",asctime(time));
        pthread_join(thread1,&ret);
        return 0;
}

在这里插入图片描述可以看出thread1先运行五次,后主线程等待超时后继续运行。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值