当前位置: 首页 > news >正文

2023-4-10-用Pthreads计算积分



🍿*★,°*:.☆( ̄▽ ̄)/$:*.°★* 🍿

💥💥💥欢迎来到🤞汤姆🤞的csdn博文💥💥💥
💟💟喜欢的朋友可以关注一下,下次更新不迷路💟💟
😆😆😆私聊获取个人订阅号哦,欢迎订阅共同学习😆😆😆
💖💖💖💖可以加入大家庭群聊,一起学习天天有福利💖💖💖💖





🍬本文摘要

在这里插入图片描述
用Pthreads计算积分的一个小例子
说明:编写一个Pthreads程序使用梯形积分求出函数𝑓(𝑥)=𝑥
2+𝑥 在区间[𝑎,𝑏]的定积分。使
用一个共享变量来表示所有计算线程的总和。在程序中使用忙等待,互斥量和信号量三种来保
证临界区的互斥。命令行如下编译:

$./pth_trap <number of threads> <number of method>

目录

  • 🍬本文摘要
  • 😉一、代码展示
  • 🐱‍🐉二、运行结果



😉一、代码展示

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>int thread_count, flag = 0;
long n, order, start_time, end_time, a, b;
double h, sum_waiting, sum_mutex, sum_semaphores;
pthread_mutex_t mutex;
sem_t semaphore;void *Thread_sum_waiting(void *rank);void *Thread_sum_mutex(void *rank);void *Thread_sum_semaphores(void *rank);int main() {long i;pthread_t *thread_handles;scanf("%d %d", &thread_count, &order);if (order == 1) {printf("Using method busy-wait\n");} else if (order == 2) {printf("Using method mutex\n");} else {printf("Using method signal\n");}thread_handles = (pthread_t *) malloc(thread_count * sizeof(pthread_t));printf("Input the value of a,b,n:\n");scanf("%d %d %d", &a, &b, &n);h = (b - a) * 1.0 / n;//求出来区间分为n份之后每一份的长度switch (order) {case 1:sum_waiting = 0;start_time = clock();for (i = 0; i < thread_count; i++)pthread_create(&thread_handles[i], NULL, Thread_sum_waiting, (void *) i);for (i = 0; i < thread_count; i++)pthread_join(thread_handles[i], NULL);end_time = clock();printf("Estimate of the integral:%lf\n", sum_waiting);//CLOCKS_PER_SEC);break;case 2:sum_mutex = 0;start_time = clock();pthread_mutex_init(&mutex, NULL);for (i = 0; i < thread_count; i++)pthread_create(&thread_handles[i], NULL, Thread_sum_mutex, (void *) i);for (i = 0; i < thread_count; i++)pthread_join(thread_handles[i], NULL);pthread_mutex_destroy(&mutex);end_time = clock();printf("Estimate of the integral:%lf\n", sum_mutex);//CLOCKS_PER_SEC);break;case 3:sum_semaphores = 0;start_time = clock();sem_init(&semaphore, 0, 1);for (i = 0; i < thread_count; i++)pthread_create(&thread_handles[i], NULL, Thread_sum_semaphores, (void *) i);for (i = 0; i < thread_count; i++)pthread_join(thread_handles[i], NULL);sem_destroy(&semaphore);end_time = clock();printf("Estimate of the integral:%lf\n", sum_semaphores);//CLOCKS_PER_SEC);break;}}void *Thread_sum_waiting(void *rank) {long my_rank = (uintptr_t) rank;long long i;double a, b;long long my_n = n / thread_count;long long my_first_i = my_n * my_rank;long long my_last_i = my_first_i + my_n;for (i = my_first_i; i < my_last_i; i++) {a = (i * h) * (i * h) + (i * h);//x2+xb = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+xwhile (flag != my_rank);sum_waiting += (a + b) * h / 2;flag = (flag + 1) % thread_count;}
}void *Thread_sum_mutex(void *rank) {long my_rank = (uintptr_t) rank;long long i;double a, b;long long my_n = n / thread_count;long long my_first_i = my_n * my_rank;long long my_last_i = my_first_i + my_n;for (i = my_first_i; i < my_last_i; i++) {a = (i * h) * (i * h) + (i * h);//x2+xb = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+xpthread_mutex_lock(&mutex);sum_mutex += (a + b) * h / 2;pthread_mutex_unlock(&mutex);}
}void *Thread_sum_semaphores(void *rank) {long my_rank = (uintptr_t) rank;long long i;double a, b;long long my_n = n / thread_count;long long my_first_i = my_n * my_rank;long long my_last_i = my_first_i + my_n;for (i = my_first_i; i < my_last_i; i++) {a = (i * h) * (i * h) + (i * h);//x2+xb = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+xsem_wait(&semaphore);sum_semaphores += (a + b) * h / 2;sem_post(&semaphore);}
}

🐱‍🐉二、运行结果

在这里插入图片描述







更多好文推荐

🍸2021-4月Python 机器学习——中文新闻文本标题分类
🍹2021年4月-(计算机网络)小型校园网络模拟搭建,最全最准确版
🍺2022-10-31-基于用户的协同过滤推荐算法实现+MAE+RMSE
🍻2022-11-28-大数据可视化,特征维度大于50
🥂2023-3-9-一篇简短的文章把C++左右值关系讲的透透彻彻

上一篇
End
下一篇
http://www.lryc.cn/news/58504.html

相关文章:

  • 什么是js?js的基本使用
  • 自然数的拆分问题 字典序
  • 软件测试——概念篇
  • Qsort函数的应用与讲解
  • 蓝桥杯嵌入式第九届客观题解析
  • 多元函数的基本概念——“高等数学”
  • LabVIEW-数值控件和布尔控件
  • R730重组阵列raid5
  • Verilog学习笔记3——数据位宽、阻塞/非阻塞赋值、二进制码、独热码、格雷码比较、编写原则、三态门、
  • C++ Qt智能指针使用教程
  • 【题解】BZOJ4975 区间翻转
  • 火箭参数相关知识
  • 【JavaEE】死锁是什么?如何避免死锁(保姆级讲解)
  • JS 实现占位符截取字符串内容
  • Prophet学习(四)趋势Changepoints
  • 超表面学习 初步印象
  • 脂肪肝 肾结石 怎么得来的
  • Python 进阶指南(编程轻松进阶):一、处理错误和寻求帮助
  • windows服务器自带IIS搭建网站并发布公网访问【内网穿透】
  • IFPUG功能点度量4:度量事务功能
  • 未来公寓智能化设计平台项目(上)
  • Java8新特性 Steam流
  • Unity 实现大世界地图的技术原理
  • jQuery制作一个简单的打地鼠游戏(超详细讲解)
  • typora和C51开发环境
  • linux echo彩色打印
  • 2023年4月PMP®项目管理专业人士认证招生简章
  • Java每日一练(20230410)
  • 主动配电网故障恢复的重构与孤岛划分统一模型研究【升级版本】(Matlab代码实现)
  • TS2023年面试题汇总~~~~持续更新中!!!!