【Linux】线程同步与互斥

线程同步与互斥

1、mutex(互斥量)

(1)初始化互斥量

第一种方法:静态分配
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER

第二种方法 :动态分配
int pthread_mutex_init(pthread_mutex_t* restrict mutex,const pthread_mutexattr_t *restrict attr);
参数:
mutex :要初始化的互斥量;
attr:NULL

(2)销毁互斥量
int pthread_mutex_destory(pthread_mutex_t* mutex);
注:
a. 使用PTHREAD_MUTEX_INITIALIZER初始化的互斥量不需要销毁。
b. 不要销毁一个已经加锁的互斥量。
c. 已经销毁的互斥量,要确保后面不会再尝试加锁。

(3)互斥量加锁和解锁

int pthread_mutex_lock(pthread_mutex_t * mutex); //加锁

int pthread_mutex_unlock(pthread_mutex_t * mutex); //解锁

返回值:成功返回0,失败返回错误号

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

没有加互斥量的代码
//int ticket=100;
//
//void* route(void* arg)
//{
//    char* id=(char*)arg;
//    while(1)
//    {
//        if(ticket>0)
//        {
//            sleep(1);
//            printf("%s sells ticket: %d\n",id,ticket);
//            ticket--;
//        }
//        else
//        {
//            break;
//        }
//    }
//}

//加互斥量的代码
int ticket=100;
pthread_mutex_t mutex;

void* route(void* arg)
{
    char* id=(char*)arg;
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(ticket>0)
        {
            printf("%s sells ticket %d\n",id,ticket);
            ticket--;
            pthread_mutex_unlock(&mutex);
            usleep(100);
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            break;
        }
    }
}

int main()
{
    pthread_t t1,t2,t3,t4;
    //pthread_mutex_init(&mutex,NULL);

    pthread_create(&t1,NULL,route,"thread 1");
    pthread_create(&t2,NULL,route,"thread 2");
    pthread_create(&t3,NULL,route,"thread 3");
    pthread_create(&t4,NULL,route,"thread 4");

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    pthread_join(t3,NULL);
    pthread_join(t4,NULL);

    //pthread_mutex_destroy(&mutex);
    return 0;
}

2、条件变量

//1.初始化
int pthread_cond_init(pthread_cond_t* restrict cond,const pthread_condattr_t *restrict attr);
//cond:要初始化的条件变量              attr:NULL

//2.销毁
int pthread_cond_destory(pthread_cond_t *cond)

//3.等待条件满足
int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
//cond: 在cond这个条件变量上等待              mutex:互斥量

//4.唤醒等待
int pthread_cond_broadcast(pthread_cond_t* cond);
int ptnread_cond_signal(pthread_cond_t* cond);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>

pthread_cond_t cond;
pthread_mutex_t mutex;

void* rout(void* arg)
{
    while(1)
    {
        pthread_cond_wait(&cond,&mutex);
        printf("activity\n");
    }

}

void* rout2(void* arg)
{
    while(1)
    {
        pthread_cond_signal(&cond);
        sleep(2);
    }
}

int main()
{
    pthread_t t1,t2;

    pthread_cond_init(&cond,NULL);
    pthread_mutex_init(&mutex,NULL);

    pthread_create(&t1,NULL,rout,NULL);
    pthread_create(&t2,NULL,rout2,NULL);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值