趣谈Linux操作系统随笔——6.0 线程:如何让复杂的项目并行执行

线程:如何让复杂的项目并行执行

  • 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
  • 开发环境:Linux-4.19-rc3内核,glibc-2.9


1、简单线程的编写与逻辑顺序

  1. 其逻辑图如下所示:

  2. 代码如下:

    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    static void* thread_func(void *arg)
    {
    	char *ac_msg = (char *)arg;
    	char cp_msg[] = "received!";
    	printf("%s\n", ac_msg);
    	printf("%s\n", cp_msg);
    
    	/** 向主线程发送数据 */
    	pthread_exit((void *)1);
    }
    
    void main(void)
    {
    	char msg[] = "收到请回答";
    	int flag = 0;
    
    	/** 声明线程 */
    	pthread_t thread;
    
    	/* 声明属性 */
    	pthread_attr_t thread_attr;
    
    	/** 创建线程:子线程回立刻调用 */
    	pthread_create(&thread, NULL, &thread_func, (void *)(&msg));
    
    	/* 注销属性 */
    	// pthread_attr_destroy(&thread_attr);
    
    	/* 等待线程退出 */
    	pthread_join(thread, (void **)&flag);
    
    	if (flag)
    	{
    		printf("子线程执行完毕,且接收到数据\n");
    	}
    	else
    	{
    		printf("子线程执行完毕,未接收到数据\n");
    	}
    
    	pthread_exit(NULL);
    }
    
    

2、添加锁机制后的程序编写与逻辑顺序

  1. 逻辑图

  2. 代码:

    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    /* 声明共享变量 */
    static int share_dsc = 1;
    
    /* 声明互斥锁 */
    static pthread_mutex_t g_mutex;
    
    static void* thread_func(void *arg)
    {
    	char *ac_msg = (char *)arg;
    	char cp_msg[] = "received!";
    	printf("%s\n", ac_msg);
    	printf("%s\n", cp_msg);
    
    	/* 加锁 */
    	pthread_mutex_lock(&g_mutex);
    
    	/* 操作共享变量 */
    	printf("子线程抢到资源share_dsc:%d\n", ++share_dsc);
    
    	/* 解锁 */
    	pthread_mutex_unlock(&g_mutex);
    
    	/** 向主线程发送数据 */
    	pthread_exit((void *)1);
    }
    
    void main(void)
    {
    	char msg[] = "收到请回答";
    	int flag = 0;
    
    	/* 初始化互斥锁 */
    	pthread_mutex_init(&g_mutex, NULL);
    
    	/** 声明线程 */
    	pthread_t thread;
    
    	/* 声明属性 */
    	pthread_attr_t thread_attr;
    
    	/** 创建线程:子线程回立刻调用 */
    	pthread_create(&thread, NULL, &thread_func, (void *)(&msg));
    
    	/* 加锁 */
    	pthread_mutex_lock(&g_mutex);
    
    	/* 操作共享变量 */
    	printf("主线程抢到资源share_dsc:%d\n", ++share_dsc);
    
    	/* 解锁 */
    	pthread_mutex_unlock(&g_mutex);
    
    	/* 注销属性 */
    	// pthread_attr_destroy(&thread_attr);
    
    	/* 等待线程退出 */
    	pthread_join(thread, (void **)&flag);
    
    	if (flag)
    	{
    		printf("子线程执行完毕,且接收到数据\n");
    	}
    	else
    	{
    		printf("子线程执行完毕,未接收到数据\n");
    	}
    
    	/* 销毁互斥锁 */
    	pthread_mutex_destroy(&g_mutex);
    
    	/* 主线程退出 */
    	pthread_exit(NULL);
    }
    

3、添加条件变量后的程序编写与逻辑顺序

  1. 逻辑图
    在这里插入图片描述

  2. 代码

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

/* 声明共享变量 */
static int share_dsc = 1;

/* 声明互斥锁 */
static pthread_mutex_t g_mutex;

/* 声明条件变量 */
pthread_cond_t g_cd;

/* 创建条件 */
static int g_flag = 0;

static void* thread_func(void *arg)
{
	char *ac_msg = (char *)arg;
	char cp_msg[] = "received!";
	printf("%s\n", ac_msg);
	printf("%s\n", cp_msg);

    //sleep(3);

	/* 加锁 */
	pthread_mutex_lock(&g_mutex);

    /* 判断条件是否满足:g_flag == 1 */
    while (g_flag == 0)
    {   
        printf("子线程等待条件变量\n");
        
        /* 等待通知 */
        pthread_cond_wait(&g_cd, &g_mutex);

        /* 更新条件 */
        g_flag = 1;
    }

	/* 操作共享变量 */
	printf("子线程抢到资源share_dsc:%d\n", ++share_dsc);

	/* 解锁 */
	pthread_mutex_unlock(&g_mutex);

	/** 向主线程发送数据 */
	pthread_exit((void *)1);
}

void main(void)
{
	char msg[] = "收到请回答";
	int flag = 0;

	/* 初始化互斥锁 */
	pthread_mutex_init(&g_mutex, NULL);

    /* 初始化条件变量 */
    pthread_cond_init(&g_cd, NULL);

	/** 声明线程 */
	pthread_t thread;

	/* 声明属性 */
	pthread_attr_t thread_attr;

	/** 创建线程:子线程回立刻调用 */
	pthread_create(&thread, NULL, &thread_func, (void *)(&msg));

    sleep(3);
	/* 加锁 */
	pthread_mutex_lock(&g_mutex);

	/* 操作共享变量 */
	printf("主线程抢到资源share_dsc:%d\n", ++share_dsc);

    /* 条件变量通知子线程 */
    pthread_cond_broadcast(&g_cd);
    printf("主线程已通知子线程\n");

    /* 解锁 */
	pthread_mutex_unlock(&g_mutex);

    printf("主线程已经解锁\n");

	/* 注销属性 */
	// pthread_attr_destroy(&thread_attr);

	/* 等待线程退出 */
	pthread_join(thread, (void **)&flag);

	if (flag)
	{
		printf("子线程执行完毕,且接收到数据\n");
	}
	else
	{
		printf("子线程执行完毕,未接收到数据\n");
	}

	/* 销毁互斥锁 */
	pthread_mutex_destroy(&g_mutex);

    /* 销毁条件变量 */
	pthread_cond_destroy(&g_cd);

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

4、总结

02a774d7c0f83bb69fec4662622d6d58

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值