Linux多线程编程学习代码(代码已上传gitee,还请各位兄弟点个Star哦!)
https://gitee.com/chenshao777/linux_thread.git
笔记:
1.条件变量引入
需要一种机制,当互斥量被锁柱后,发现当前线程还是无法完成自己的操作
那么它应该自己释放互斥量,让其他线程得以工作
方法1:采用轮询方式
方法2:让系统查询条件,使用系统条件变量 pthread_cond_t cond
2.条件变量初始化和销毁
条件变量使用之前需要初始化
1、pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2、int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr)
第一个参数是条件变量
第二个参数默认属性为空NULL
条件变量使用完成之后需要销毁
int pthread_cond_destroy(pthread_cond_t *cond);
3.条件变量的使用
条件变量使用雷要配合互斥量
int pthread_cond_wait(pthread_cond_t *restrict cond.
pthread_mutex_t *restrict mutex);
1、使用pthread_cond_wait等待条件变为真。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传递给函数。
2、这个函数将线程放到等待条件的线程列表上,然后对互斥量进行解锁,这是个原子操作。当条件满足时这个函数返回,返回以后维续对互斥量加锁。
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
3、这个函数与pthread_cond_wait类似,只是多一个timeout,如果到了指定的时间条件还不满足,那么就返回。
时间用下面的结构体表示
struct timespec{
time_t tv_sec;
long tv_nsec;
}:
注意。这个时间是绝对时间。例如你要等待3分钟,就要把当前时间加上3分钟然后转换到timespec·而不是直接将3分钟转换到timespec
当条件满足的时候,需要唤醒等侍条件的线程
int pthread_cond_broadcast(pthread_cond_t *cond);
Iint pthread_cond_signal(pthread_cond_t *cond);
1、pthread_cond_broadcast 唤醒等待条件的所有线程(广播)
2、pthread_cond_signal 至少唤醒等待条件的某一个线程
注意,一定要在条件改变以后在唤醒线程
spu.h文件
#ifndef _SPU_H_
#define _SPU_H_
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
#include<errno.h>
#endif
10.thread_cond.c文件
1.创建两个线程,都对 money 全局变量进行操作
2.线程1每次对 money 加10
3.线程2每次对 money 加0.01
4.如果出现互斥的情况,线程1会输出小数
#include "spu.h"
#include <time.h>
typedef struct product{
int num;
struct product *next;
}product_t;
product_t *head = NULL;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *thread_product(void *arg)
{
for(;;)
{
product_t *p = (product_t*)malloc(sizeof(product_t));
p->num = rand() % 100 + 1;
pthread_mutex_lock(&mutex);
p->next = head;
head = p;
printf("生产者:%d\n",head->num);
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
usleep(500000);
}
}
void *thread_consume01(void *arg)
{
for(;;)
{
pthread_mutex_lock(&mutex);
if(head->next == NULL)
pthread_cond_wait(&cond, &mutex);
printf("消费者1:%d\n", head->num);
head = head->next;
pthread_mutex_unlock(&mutex);
usleep(1000000);
}
}
void *thread_consume02(void *arg)
{
for(;;)
{
pthread_mutex_lock(&mutex);
if(head->next == NULL)
pthread_cond_wait(&cond, &mutex);
printf("消费者2:%d\n", head->num);
head = head->next;
pthread_mutex_unlock(&mutex);
usleep(1000000);
}
}
int main()
{
srand((unsigned int)time(NULL));
pthread_t pro_pid, con_pid1, con_pid2;
void *_return1,*_return2,*_return3;
pthread_create(&pro_pid, NULL, thread_product, NULL);
pthread_create(&con_pid1, NULL, thread_consume01, NULL);
pthread_create(&con_pid2, NULL, thread_consume02, NULL);
pthread_join(pro_pid,_return1);
pthread_join(con_pid1,_return2);
pthread_join(con_pid2,_return3);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)