6.28 作业

1.互斥锁

#include<myhead.h>
int num = 5000;        //定义全局变量  临界资源
//1、定义一个锁资源
//pthread_mutex_t mutex;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  //静态初始化互斥锁

//定义线程体1
void *task1(void *arg)
{
    while(1)
    {
        //3、上锁
        pthread_mutex_lock(&mutex);

        num = num - 100;
        printf("张三取钱100元, 剩余%d\n", num);

        if(num <= 0)
        {
            printf("张三取钱结束,因为钱不够了\n");
            //释放锁资源
            pthread_mutex_unlock(&mutex);
            break;
        }
        
        //释放锁资源
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }

    pthread_exit(NULL);
}

//定义线程体2
void *task2(void *arg)
{
    while(1)
    {
        //3、给临界区上锁
        pthread_mutex_lock(&mutex);

        num = num - 50;
        printf("李四取钱50元,剩余:%d\n", num);

        if(num <= 0)
        {
            printf("李四取钱结束,因为钱不够了\n");
            //当循环结束时,也要释放锁资源
            pthread_mutex_unlock(&mutex);
            break;
        }
        
        //4、释放锁资源
        pthread_mutex_unlock(&mutex);
            sleep(1);
    }

    //退出线程
    pthread_exit(NULL);
}



int main(int argc, const char *argv[])
{
    //定义两个线程号
    pthread_t tid1, tid2;

    //创建两个线程
    if(pthread_create(&tid1, NULL, task1, NULL))
    {
        perror("create tid1 error");
        return -1;
    }

    if(pthread_create(&tid2, NULL, task2, NULL))
    {
        perror("create tid2 error");
        return -1;
    }

    //2、初始化互斥锁
    //pthread_mutex_init(&mutex, NULL);

    //回收线程资源
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    
    //5、销毁锁资源
    pthread_mutex_destroy(&mutex);
    
    return 0;
}

2.无名信号量

#include<myhead.h>

//1、定义一个无名信号量
sem_t sem;

//定义生产者线程
void *task1(void *arg)
{
    while(1)
    {
        sleep(2);

        printf("我生产了一辆大奔\n");

        //4、释放资源
        sem_post(&sem);
    }
}

//定义消费者线程
void *task2(void *arg)
{
    while(1)
    {
        sleep(1);
        
        //3、申请资源
        sem_wait(&sem);    //如果没有资源可以申请,则等待

        printf("我消费了一辆大奔\n");
    }
}


int main(int argc, const char *argv[])
{
    //定义两个线程
    pthread_t tid1,tid2;

    //创建两个线程
    if(pthread_create(&tid1, NULL, task1, NULL))
    {
        perror("create tid1 error");
        return -1;
    }

    if(pthread_create(&tid2, NULL, task2, NULL))
    {
        perror("create tid2 error");
        return -1;
    }

    //2、初始化无名信号量
    //第一个0表示该无名信号量用于线程之间
    //第二个0,表明初始值时,没有资源可以申请
    sem_init(&sem, 0, 1);

    //阻塞等待回收子线程的资源
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    //5、销毁无名信号量
    sem_destroy(&sem);

    return 0;
}

3.条件变量

#include<myhead.h>

//1、定义条件变量
pthread_cond_t cond;

//11、定义互斥锁
pthread_mutex_t mutex;

//创建生产者线程
void *task1(void *arg)
{
    while(1)
    {
        sleep(3);

        printf("我生产了三辆跑车\n");
        //3、释放资源,并通知队列中的等待线程
        //pthread_cond_signal(&cond);    //唤醒第一个等待线程
        pthread_cond_broadcast(&cond);    //唤醒所有等待线程
    }
}

//创建消费者线程
void *task2(void *arg)
{
    while(1)
    {
        sleep(1);
        
        //33、上锁
        pthread_mutex_lock(&mutex);

        //4、申请资源
        pthread_cond_wait(&cond, &mutex);

        printf("%#lx:我消费一个跑车\n", pthread_self());

        //44、解锁
        pthread_mutex_unlock(&mutex);
    }
}

int main(int argc, const char *argv[])
{
    //定义两个线程
    pthread_t tid1, tid2, tid3, tid4;

    //创建两个线程
    if(pthread_create(&tid1, NULL, task1, NULL))
    {
        perror("create tid1 error");
        return -1;
    }

    if(pthread_create(&tid2, NULL, task2, NULL))
    {
        perror("create tid2 error");
        return -1;
    }
    
    if(pthread_create(&tid3, NULL, task2, NULL))
    {
        perror("create tid3 error");
        return -1;
    }

    if(pthread_create(&tid4, NULL, task2, NULL))
    {
        perror("create tid4 error");
        return -1;
    }

    //2、初始化一个条件变量
    pthread_cond_init(&cond, NULL);
    //22、初始化互斥锁
    pthread_mutex_init(&mutex,NULL);

    printf("tid1=%#lx,tid2=%#lx,tid3=%#lx,tid4=%#lx",\
            tid1,tid2,tid3,tid4);

    //回收线程资源
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);

    //5、释放条件变量
    pthread_cond_destroy(&cond);
    //55、释放锁资源
    pthread_mutex_destroy(&mutex);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值