线程同步

线程同步概念

imageimage

互斥锁

imageimageimageimage

读写锁

imageimageimageimageimage

死锁

image

条件变量

本身不是锁,但通常结合锁来使用imageimageimage

生产者消费者模型

image

/*************************************************************************
  > File Name: producer_consumer_test.c
  > Author: shaozheming
  > Mail: 957510530@qq.com
  > Created Time: 2022年03月03日 星期四 10时43分40秒
 ************************************************************************/

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

/* 链表作为共享数据,需被互斥量保护 */
struct msg {
	struct msg *next;
	int num;
};

struct msg *head;

/* 静态初始化一个条件变量和一个互斥量 */
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *consumer(void *p)
{
	struct msg *mp;

	for(;;) {
		pthread_mutex_lock(&lock);
		while(head == NULL){
			pthread_cond_wait(&has_product, &lock);
		}
		mp = head;
		head = mp->next;   //模拟消费掉一个产品
		pthread_mutex_unlock(&lock);

		printf("--Consume %lu---%d\n", pthread_self(), mp->num);
		free(mp);
		sleep(rand() % 5);
	}
}

void *producer(void *p)
{
	struct msg *mp;
	for(;;){
		mp = malloc(sizeof(struct msg));
		mp->num = rand() % 1000 + 1;//模拟生产一个产品
		printf("-Produce--------------%d\n", mp->num);

		pthread_mutex_lock(&lock);
		mp->next = head;
		head = mp;
		pthread_mutex_unlock(&lock);

		pthread_cond_signal(&has_product); //将等待在该条件变量上的一个线程唤醒
		sleep(rand() % 5);
	}
}

void sys_err(const char *str)
{
	perror(str);
	exit(1);
}

int main(int argc, char* argv[])
{
	pthread_t pid, cid;
	srand(time(NULL));

	pthread_create(&pid, NULL, producer, NULL);
	pthread_create(&cid, NULL, consumer, NULL);

	pthread_join(pid, NULL);
	pthread_join(cid, NULL);

	return 0;
}

image

信号量

相当于初始化为N的互斥量,N值表示同时访问共享区的线程数。imageimageimageimage
相对时间:是自己当前时间为基准
绝对时间:Linux操作系统开始算生产者消费者模型image

/*************************************************************************
  > File Name: producer_consumer_test.c
  > Author: shaozheming
  > Mail: 957510530@qq.com
  > Created Time: 2022年03月03日 星期四 10时43分40秒
 ************************************************************************/

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

#define NUM 5
int queue[NUM];                     //全局数组实现环形队列
sem_t blank_number, product_number; //空格子信号量,产品信号量


void *consumer(void *arg)
{
	int i = 0;

	while(1) {
		sem_wait(&product_number);   //消费者产品数--,为0等待
		printf("----Consume----%d\n", queue[i]);
		queue[i] = 0;               //消费一个产品
		sem_post(&blank_number);    //消费掉之后,将空格子数++

		i = (i+1) % NUM;
		sleep(rand() % 3);

	}
}

void *producer(void *arg)
{
	int i = 0;

	while(1) {
		sem_wait(&blank_number);    //生产者空格子数--,为0等待
		queue[i] = rand() % 1000 + 1; //生产一个产品
		printf("----Produce----%d\n", queue[i]);
		sem_post(&product_number);   //将产品数++

		i = (i+1) % NUM;
		sleep(rand()%1);    //借助下标实现环形
	}
}

void sys_err(const char *str)
{
	perror(str);
	exit(1);
}

int main(int argc, char* argv[])
{
	pthread_t pid, cid;

	sem_init(&blank_number, 0, NUM);
	sem_init(&product_number, 0, 0);

	pthread_create(&pid, NULL, producer, NULL);
	pthread_create(&cid, NULL, consumer, NULL);

	pthread_join(pid, NULL);
	pthread_join(cid, NULL);

	sem_destroy(&blank_number);
	sem_destroy(&product_number);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值