首页 > 其他分享 >thread同步

thread同步

时间:2022-11-13 10:45:52浏览次数:34  
标签:同步 thread blank number NUM pthread sem NULL

一、代码(修改前)

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

#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );
		queue[p] = rand() % 1000;
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;
		sleep ( rand() % 5);
		sem_post( &product_number );
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;
		sleep( rand() % 5 );
		sem_post( &blank_number );
	}
}

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;
}

编译运行

image
image

2.代码(修改后)

同步资源个数为3个,使用资源的线程 (20201305%3+4)=5 个,即消费者线程增加到个数为5,且需要互斥地去访问产品(最多为三个),1个生产者线程,5个消费者线程。

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

#define NUM 3//资源的数量为3
int queue[NUM];
sem_t blank_number, product_number,mutex;//设置缓冲区空白位置数量、缓冲区产品数量、互斥信号量

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );//是否对生产者阻塞
		sem_wait( &mutex);//占用互斥信号量,互斥信号量-1
		queue[p] = rand() % 1000;//等待队列
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;//因为资源只有3个,计数后还需要mod 3
		sleep ( rand() % 5);//休眠
		sem_post(&mutex); //释放互斥信号量
		sem_post( &product_number );//是否唤醒消费者
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );//是否对消费者进行阻塞
    	sem_wait(&mutex);//互斥信号量 
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;//同理,资源数只有3个
		sleep( rand() % 5 );
		sem_post(&mutex);//互斥信号量
		sem_post( &blank_number );//是否唤醒生产者
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid1,cid2,cid3,cid4,cid5;//创建5个消费者线程
	sem_init( &blank_number, 0, NUM );//初始化空闲缓冲区信号量
	sem_init( &product_number, 0, 0);//初始化产品信号量
	sem_init( &mutex, 1, 1);//初始化互斥信号量
	pthread_create( &pid, NULL, producer, NULL);//创建生产者进程
	pthread_create( &cid1, NULL, consumer, NULL);//创建消费者线程
	pthread_create( &cid2, NULL, consumer, NULL);
	pthread_create( &cid3, NULL, consumer, NULL);
	pthread_create( &cid4, NULL, consumer, NULL);
	pthread_create( &cid5, NULL, consumer, NULL);//这里创建了5个消费者线程
	pthread_join( pid, NULL );
	pthread_join( cid1, NULL );//等待消费者线程执行完毕
	pthread_join( cid2, NULL );
	pthread_join( cid3, NULL );
	pthread_join( cid4, NULL );
	pthread_join( cid5, NULL );//5个消费者线程执行完毕
	sem_destroy( &blank_number );//销毁信号量
	sem_destroy( &product_number );
	return 0;
}

运行结果

image
image

标签:同步,thread,blank,number,NUM,pthread,sem,NULL
From: https://www.cnblogs.com/1395372955jth/p/16885517.html

相关文章

  • thread 互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片一、代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h......
  • thread同步测试
    thread同步测试任务详情1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提......
  • C# 文件同步工具(复制带进度显示)
    usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceSyncTool{internalc......
  • datax同步数据java简单用法
    1.到github下载源码,自己编译。同步数据支持mysql8.0,如果直接用编译好的会遇到各种问题。https://github.com/alibaba/DataX/blob/master/userGuid.mdidea导入项目,需要先......
  • Linux系统中线程同步方式中的条件变量操作方法
      大家好,今天主要和大家聊一聊,如何使用Linux中线程同步方式中的条件变量。    第一:条件变量基本简介   条件变量是线程可用的另一种同步机制,条件变量用于自动阻......
  • 线程同步(多个线程操作同一个资源)
    线程同步(多个线程操作同一个资源)核心概念线程是独立的执行路径在下线程运行时,即使没有自己创建线程,后台也会有多个线程,如:主线程,gc线程;main()称之为主线程,为系统的入......
  • setState同步异步
    setState何时同步何时异步?点击查看代码1、setState只在合成事件(react为了解决跨平台,兼容性问题,自己封装了一套事件机制,代理了原生的事件,像在jsx中常见的onClick......
  • 自有服务器搭建git并同步到网站根目录,实现git更新网站
    一.GIT安装yuminstall-ygit服务器端创建git用户[root@localhosthome]#idgitid:git:无此用户[root@localhosthome]#useraddgit[root@localhosthom......
  • MIT 6.828 Homework: Thread and locking
    任务:修改代码以实现对于每一个循环,让每一个线程都暂时阻塞直到所有线程都调用了barier函数实际上就是实现一个屏障,当线程运行到屏障之前,会被暂时挂起,直到所有线程都到达屏......
  • mysql-canal-kafka-kettle 数据实时同步链部署bug 填坑过程
     1,因为mysql版本从5.7提高到8.0 ,需要更改用户配置。createuser'canal'@'%'identifiedby'canal';grantselect,replicationslave,replicationclienton......