I/O作业6

写一遍多线程编程相关的代码

1>代码:

#include <myhead.h>
 
// 1、创建一个互斥锁
pthread_mutex_t mutex;
 
int num = 20; // 票的个数
 
// 定义线程体1
void *task1(void *arg)
{
        while (1)
        {
                // 3、获取锁资源
                pthread_mutex_lock(&mutex);
                if (num > 0)
                {
 
                        usleep(1000);
                        num--;
                        printf("张三买了一张票,剩余:%d\n", num);
                }

                else
                {
                        printf("票已经售完,购票失败\n");
                        // 4、释放锁资源
                        pthread_mutex_unlock(&mutex);
                        break;                }
                // 4、释放锁资源
                pthread_mutex_unlock(&mutex);
        }
 
        pthread_exit(NULL); // 退出线程
}
 
// 定义线程体函数2
void *task2(void *arg)
{
        while (1)
        {
                // 3、获取锁资源
                pthread_mutex_lock(&mutex);
                if (num > 0)
                {
 
                        usleep(1000);
                        num--;
                        printf("李四买了一张票,剩余:%d\n", num);
 
                }
                else
                {
                        printf("票已经售完,购票失败\n");
                        // 4、释放锁资源
                        pthread_mutex_unlock(&mutex);
                        break;
                }
                // 4、释放锁资源
                pthread_mutex_unlock(&mutex);
        }
 
        pthread_exit(NULL); // 退出线程
}
 
// 定义线程体函数3
void *task3(void *arg)
{
        while (1)
        {
                // 3、获取锁资源
                pthread_mutex_lock(&mutex);
                if (num > 0)
                {
                        usleep(1000);
                        num--;
                        printf("王五买了一张票,剩余:%d\n", num);
 
                }
                else
                {
                        printf("票已经售完,购票失败\n");
                        // 4、释放锁资源
                        pthread_mutex_unlock(&mutex);
                        break;
                }
 
                // 4、释放锁资源
                pthread_mutex_unlock(&mutex);
        }
 
        pthread_exit(NULL); // 退出线程
}
 
/******************主程序**********************/
int main(int argc, const char *argv[])
{
 
        // 2、初始化互斥锁
        pthread_mutex_init(&mutex, NULL);
 
        // 创建两个任务
        pthread_t tid1, tid2, tid3;
 
        if (pthread_create(&tid1, NULL, task1, NULL) != 0)
        {
                printf("task1 创建失败\n");
                return -1;
        }
        if (pthread_create(&tid2, NULL, task2, NULL) != 0)
        {
                printf("task2 创建失败\n");
                return -1;
        }
        if (pthread_create(&tid3, NULL, task3, NULL) != 0)
        {
                printf("task3 创建失败\n");
                return -1;
        }
        // 主线程
        printf("tid1 = %#lx, tid2 = %#lx\n", tid1, tid2);
 
        // 阻塞回收线程资源
        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
        pthread_join(tid3, NULL);
 
        // 5、销毁锁资源
        pthread_mutex_destroy(&mutex);
        return 0;
}

运行结果:

2>代码:

#include<myhead.h>
 
//1、定义无名信号量
sem_t sem;           //创建一个无名信号量
 
//创建生产者线程
void * task1(void *arg)
{
    int num = 5;
    while(num--)
    {
        sleep(1);
        printf("%#lx:生产了一辆特斯拉\n", pthread_self());
 
        //4、释放资源,表示消费者可以执行了
        sem_post(&sem);
    }
 
    //退出线程
    pthread_exit(NULL);
}
 
//创建消费者线程
void * task2(void *arg)
{
    int num = 5;
    while(num--)
    {
        //3、申请资源
        sem_wait(&sem);
 
        printf("%#lx:消费了一辆特斯拉\n", pthread_self());
        
    }
 
    //退出线程
    pthread_exit(NULL);
}
 
/*******************主程序********************* */
int main(int argc, char const *argv[])
{
 
    //2、初始化无名信号量
    sem_init(&sem, 0, 0);
    //第一个0表示同步用于多线程之间
    //第二个0表示无名信号量的初始资源为0
 
    //创建两个线程
    pthread_t tid1, tid2;
    if(pthread_create(&tid1, NULL, task1, NULL) != 0)
    {
        printf("tid1 create error\n");
        return -1;
    }
    if(pthread_create(&tid2, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -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)
{
    // int num = 5;
    // while (num--)
    // {
    //     sleep(1);
    //     printf("%#lx:生产了一辆特斯拉\n", pthread_self());
 
    //     //4、唤醒消费者线程开始执行
    //     pthread_cond_signal(&cond);
    // }
 
    sleep(3);        //等待3秒
 
    printf("我生产了5辆特斯拉\n");
 
    //唤醒所有消费者线程
    pthread_cond_broadcast(&cond);
 
 
    // 退出线程
    pthread_exit(NULL);
}
 
// 创建消费者线程
void *task2(void *arg)
{
    //33、获取互斥锁
    pthread_mutex_lock(&mutex);
 
    //3、请求进入等待队列
    pthread_cond_wait(&cond, &mutex);
 
    printf("%#lx:消费了一辆特斯拉\n", pthread_self());
 
    //44、释放锁资源
    pthread_mutex_unlock(&mutex);
 
    // 退出线程
    pthread_exit(NULL);
}
 
/*******************主程序********************* */
int main(int argc, char const *argv[])
{
 
    //2、初始化条件变量
    pthread_cond_init(&cond, NULL);
    //22、初始化互斥锁
    pthread_mutex_init(&mutex, NULL);
 
    // 创建两个线程
    pthread_t tid1, tid2, tid3, tid4, tid5, tid6;
    if (pthread_create(&tid1, NULL, task1, NULL) != 0)
    {
        printf("tid1 create error\n");
        return -1;
    }
    if (pthread_create(&tid2, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
    if (pthread_create(&tid3, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
    if (pthread_create(&tid4, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
    if (pthread_create(&tid5, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
    if (pthread_create(&tid6, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
 
    //输出每个线程的线程号
    printf("tid2=%#lx, tid3=%#lx, tid4=%#lx, tid5=%#lx, tid6=%#lx\n", tid2,tid3, tid4, tid5, tid6);
 
    // 阻塞回收线程的资源
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);
    pthread_join(tid5, NULL);
    pthread_join(tid6, NULL);
 
    //5、销毁条件变量
    pthread_cond_destroy(&cond);
 
    //55、销毁锁资源
    pthread_mutex_destroy(&mutex); 
 
 
    return 0;

}

运行结果:

4>思维导图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值