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

简介: 2023-4-10-用Pthreads计算积分

用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+x
        b = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+x
        while (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+x
        b = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+x
        pthread_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+x
        b = ((i + 1) * h) * ((i + 1) * h) + ((i + 1) * h);//x2+x
        sem_wait(&semaphore);
        sum_semaphores += (a + b) * h / 2;
        sem_post(&semaphore);
    }
}

🐱‍🐉二、运行结果



目录
打赏
0
0
0
0
5
分享
相关文章
探索微服务架构中的服务网格Service Mesh
【10月更文挑战第7天】服务网格(Service Mesh)是微服务架构中的关键组件,通过在每个服务实例旁部署Sidecar代理,实现服务间通信的管理、监控和安全增强。本文介绍了服务网格的基本概念、核心组件、优势及实施步骤,探讨了其在现代开发中的应用,并提供了实战技巧。
自注意力机制全解析:从原理到计算细节,一文尽览!
自注意力机制(Self-Attention)最早可追溯至20世纪70年代的神经网络研究,但直到2017年Google Brain团队提出Transformer架构后才广泛应用于深度学习。它通过计算序列内部元素间的相关性,捕捉复杂依赖关系,并支持并行化训练,显著提升了处理长文本和序列数据的能力。相比传统的RNN、LSTM和GRU,自注意力机制在自然语言处理(NLP)、计算机视觉、语音识别及推荐系统等领域展现出卓越性能。其核心步骤包括生成查询(Q)、键(K)和值(V)向量,计算缩放点积注意力得分,应用Softmax归一化,以及加权求和生成输出。自注意力机制提高了模型的表达能力,带来了更精准的服务。
DNS缓存中毒
【8月更文挑战第20天】
381 1
如何使用 PHP Simple HTML DOM Parser 轻松获取网页中的特定数据
本文介绍了使用PHP Simple HTML DOM Parser进行网页数据抓取的方法,尤其适用于从懂车帝二手车网站提取汽车品牌、价格和里程等关键信息。首先,安装并配置所需库,使用代理IP和设置cookie与useragent来模拟用户行为,避免被封。然后,通过编写PHP脚本,利用cURL获取网页内容,解析HTML并提取所需数据,最终将数据保存至CSV文件。文章强调了正确配置代理和用户代理的重要性,并提供了完整的PHP代码示例,以帮助读者理解和应用网页抓取技术。
186 0
如何使用 PHP Simple HTML DOM Parser 轻松获取网页中的特定数据
硬盘坏道如何检测和修复?
本文介绍了硬盘坏道的概念,包括逻辑坏道和物理坏道的区别,并提供了使用DiskGenius检测和修复坏道的步骤。当硬盘出现坏道且包含重要数据时,应立即备份数据,使用数据恢复软件,或在严重情况下寻求专业帮助。保护和恢复数据是应对硬盘坏道的关键。
【SPSS】单因素方差分析详细操作教程(附案例实战)
【SPSS】单因素方差分析详细操作教程(附案例实战)
3340 0
【SPSS】单因素方差分析详细操作教程(附案例实战)
JVM从入门到入土之详解G1垃圾回收器
前言 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/bin392328206/six-finger
626 0
nacos每次springboot 升级 是不是cloud 与 cloud alibaba 也得升级呢?非常不幸的是 nacos 跟不上 s-boot的关系速度,你们认为我说的对么?
nacos每次springboot 升级 是不是cloud 与 cloud alibaba 也得升级呢?非常不幸的是 nacos 跟不上 s-boot的关系速度,你们认为我说的对么?
164 2
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问