线程:如何让复杂的项目并行执行
- 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
- 开发环境:Linux-4.19-rc3内核,glibc-2.9
1、简单线程的编写与逻辑顺序
-
其逻辑图如下所示:
-
代码如下:
#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、添加锁机制后的程序编写与逻辑顺序
-
逻辑图
-
代码:
#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、添加条件变量后的程序编写与逻辑顺序
-
逻辑图
-
代码
#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);
}