首页 > 其他分享 >thread互斥测试

thread互斥测试

时间:2022-11-13 17:57:29浏览次数:46  
标签:count thread lock mailbox 互斥 mutex 测试 pthread 线程

thread互斥测试

实践代码

#include  <stdio.h>
#include  <stdlib.h>
#include  <pthread.h> //linux 线程库
#include  <ctype.h>   //测试和映射字符的库

struct arg_set {
		char *fname;
		int  count;
};//定义结构体

struct arg_set  *mailbox = NULL;//定义结构体指针;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;//linux线程创建锁,初始化锁
pthread_cond_t  flag = PTHREAD_COND_INITIALIZER; //初始化条件变量,进行静态初始化

void *count_words(void *);//定义函数指针
int main(int argc, char *argv[])
{
	pthread_t t1, t2; //进行多线程管理
	struct arg_set args1, args2;//初始化两个结构体
	int reports_in = 0;
	int	total_words = 0;

	if ( argc != 3 ){
		printf("usage: %s file1 file2\n", argv[0]);
		exit(1);
	}//判断传入参量,参量的数量决定输出用法

	args1.fname = argv[1];//args1结构体指向第二个变量
	args1.count = 0;//args1结构体初始化count
	pthread_create(&t1, NULL, count_words, (void *) &args1);//在执行中创建一个线程, 为该线程分配它需要做的工作(线程执行函数), 该线程共享进程的资源

	args2.fname = argv[2];//args2结构体指向第三个变量
	args2.count = 0;//args2结构体count初始化
	pthread_create(&t2, NULL, count_words, (void *) &args2);//创建线程,并分配需要做的工作和资源,分配共享进程的资源

	pthread_mutex_lock(&lock);//加锁
	while( reports_in < 2 ){
		printf("MAIN: waiting for flag to go up\n");
		pthread_cond_wait(&flag, &lock);//阻塞当前线程,等待别的线程使用 
		printf("MAIN: Wow! flag was raised, I have the lock\n");
		printf("%7d: %s\n", mailbox->count, mailbox->fname);
		total_words += mailbox->count;//加mailbox的count值
		if ( mailbox == &args1) 
			pthread_join(t1,NULL);
        //阻塞调用它的线程,直至目标线程执行结束
		if ( mailbox == &args2) 
			pthread_join(t2,NULL);
		mailbox = NULL;
		pthread_cond_signal(&flag);	
        //发送一个信号,给另外一个正在处于阻塞等待状态的线程,使其脱离阻塞状态
		reports_in++;
	}
	pthread_mutex_unlock(&lock);
	//解除锁定mutex所指向的互斥锁的函数
	printf("%7d: total words\n", total_words);
}
void *count_words(void *a)
{
	struct arg_set *args = a;
    //初始化结构体,指向传参a;
	FILE *fp;
	int  c, prevc = '\0';
	
	if ( (fp = fopen(args->fname, "r")) != NULL ){
        //成功打开文件
        
		while( ( c = getc(fp)) != EOF ){
			if ( !isalnum(c) && isalnum(prevc) )
                //isalnum检测字符串是否由字母和数字组成。
				args->count++;
                //指向的数值count增加1
			prevc = c;
            
		}
		fclose(fp);
	} else 
		perror(args->fname);
	printf("COUNT: waiting to get lock\n");
	pthread_mutex_lock(&lock);//上锁	
	printf("COUNT: have lock, storing data\n");
	if ( mailbox != NULL ){
		printf("COUNT: oops..mailbox not empty. wait for signal\n");
		pthread_cond_wait(&flag,&lock);
	}
	mailbox = args;			
	printf("COUNT: raising flag\n");
	pthread_cond_signal(&flag);	
	printf("COUNT: unlocking box\n");
	pthread_mutex_unlock(&lock);	
	return NULL;
}


实践截图

原理理解

  • 在线程控制中首要任务是何以分配资源互斥锁的运用可以合理的避免资源过度浪费的为题。
    互斥锁使用过程步骤
  • 1.锁的创建 函数原型int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * attr)
  • 2.初始化锁的属性 函数原型pthread_mutexattr_init(pthread_mutexattr_t *mattr)
  • 3.释放锁资源 函数原型pthread_mutex_destory
  • 4.进行锁操作 函数原型
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex)

代码实现意义

使用互斥锁来查看两个文件中字符串的数量,先输入的1.txt文件先被加锁,2.txt文件需等待,文件1.txt完成之后再进行文件2.txt的统计,最后输出总统计结果。

标签:count,thread,lock,mailbox,互斥,mutex,测试,pthread,线程
From: https://www.cnblogs.com/hzxjkl/p/16885839.html

相关文章

  • iOS 启动优化测试
    APP的启动在iOS中,讨论的APP的启动可以分为2种:冷启动(ColdLaunch):从零开始启动APP热启动(WarmLaunch):APP已经在内存中,在后台存活着,再次点击图标启动APP主要是针对冷启......
  • 拿下阿里自动化测试岗23k*14薪offer的全程面试记录解析以及总结
    一、自我介绍面试官您好!我叫xx,来自深圳,毕业之后一直从事于软件测试的工作,有做过保险、金融、电商等项目;有做过做功能测试、接口测试,自动化测试,在工作中积极主动、可以独立的......
  • 【转行测试开发-HTML】(四)---对标签功能的一个重要补充
    之前写HTML基础知识时,不小心忽略掉了很多基础功能没有说,今天一并给大家补上。 1.HTML编辑工具   我现在使用的是sublimetext,如果未来会做前端开发,或者测试开发需......
  • thread同步测试
    任务详情1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码......
  • (译)TDD(测试驱动开发)的5个步骤
    原文:5stepsoftest-drivendevelopmenthttps://developer.ibm.com/articles/5-steps-of-test-driven-development/作者GrantSteinfeld发表于2020年2月6日......
  • 【博学谷学习记录】超强总结,用心分享|Requests库以及集成UnitTest实现接口测试案例总
    一,介绍Requests库是一个基于python语言开发的一个开源的库,能够完全满足基于HTTP协议的接口测试。二,安装与验证Requests库的安装:在cmd窗口输入:pipinstallrequests......
  • BLYNK 之http api 测试
    搭了个BLYNK服务器想了解BLYNK之httpapi,由于所搭建的BLYNK服务器版本和当前blynk官网的版本不一致,没有找到相应资料。最后在网上找到了一篇https://blog.csdn.net/u0136......
  • thread同步
    一、代码(修改前)#include<stdio.h>#include<pthread.h>#include<stdlib.h>#include<semaphore.h>#defineNUM5intqueue[NUM];sem_tblank_number,product_nu......
  • thread 互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片一、代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h......
  • 抖音"凶猛"的幕后英雄,火山引擎 DataTester 累计做过 150 万次 A/B 测试
    在国内互联网领域,字节跳动是最为推崇A/B测试的公司,旗下“抖音”、“今日头条”两大最著名产品,连APP的名字都是来源于A/B测试。A/B测试(也叫AB实验)也被称为对照实......