多线程计算PI代码c++
时间: 2023-09-24 14:06:48 浏览: 205
### 回答1:
以下是多线程计算PI的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREADS_NUM 4
#define STEPS 100000000
double pi = .;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *calculate_pi(void *arg) {
int thread_id = *(int *)arg;
double sum = .;
int start = thread_id * STEPS / THREADS_NUM;
int end = (thread_id + 1) * STEPS / THREADS_NUM;
for (int i = start; i < end; i++) {
double x = (i + .5) / STEPS;
sum += 4. / (1. + x * x);
}
pthread_mutex_lock(&mutex);
pi += sum;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t threads[THREADS_NUM];
int thread_ids[THREADS_NUM];
for (int i = ; i < THREADS_NUM; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, calculate_pi, &thread_ids[i]);
}
for (int i = ; i < THREADS_NUM; i++) {
pthread_join(threads[i], NULL);
}
printf("PI = %.15f\n", pi / STEPS);
return ;
}
### 回答2:
多线程计算PI的代码可以如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
#define NUM_ITERATIONS 10000000
double sum[NUM_THREADS] = {0.0};
void *calculate_pi(void *thread_id) {
long tid = (long)thread_id;
int start = tid * NUM_ITERATIONS / NUM_THREADS;
int end = (tid + 1) * NUM_ITERATIONS / NUM_THREADS;
double result = 0.0;
for (int i = start; i < end; i++) {
double factor = 1.0 / (2 * i + 1);
if (i % 2 == 0) {
result += factor;
} else {
result -= factor;
}
}
sum[tid] = result;
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
for (long t = 0; t < NUM_THREADS; t++) {
int rc = pthread_create(&threads[t], NULL, calculate_pi, (void *)t);
if (rc) {
printf("Error: unable to create thread, %d\n", rc);
exit(-1);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
double pi = 0.0;
for (int i = 0; i < NUM_THREADS; i++) {
pi += sum[i];
}
pi *= 4.0;
printf("PI: %f\n", pi);
pthread_exit(NULL);
}
```
该代码使用多线程来计算PI的近似值。首先定义了4个线程和迭代次数为10000000。然后创建4个线程,并将计算PI的函数分配给每个线程执行。每个线程计算自己负责的一部分迭代,最后将结果存储在全局数组sum中。主线程等待所有线程完成后,将各个线程的结果累加起来,并进行一些计算得到PI的近似值。最后打印出PI的值。
### 回答3:
多线程计算PI的c代码如下:
```c
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4 // 线程数量
#define STEPS 1000000 // 迭代次数
double pi = 0.0; // PI的结果
double step; // 每次迭代的步长
// 线程函数
void *calculatePI(void *param) {
int thread_id = *(int*)param; // 获取线程ID
int start = thread_id * STEPS / NUM_THREADS; // 计算起始迭代位置
int end = (thread_id + 1) * STEPS / NUM_THREADS; // 计算结束迭代位置
// 进行迭代求解
double sum = 0.0;
for (int i = start; i < end; i++) {
double x = (i + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}
// 将线程计算的结果加到全局变量pi中
pi += sum * step;
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_id[NUM_THREADS];
step = 1.0 / STEPS; // 计算步长
// 创建多个线程进行计算
for (int i = 0; i < NUM_THREADS; i++) {
thread_id[i] = i;
pthread_create(&threads[i], NULL, calculatePI, &thread_id[i]);
}
// 等待所有线程完成计算
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// 输出最终结果
printf("PI = %.15f\n", pi);
return 0;
}
```
以上代码使用多线程方式计算PI的近似值。首先确定迭代次数和要使用的线程数量,然后通过线程函数进行迭代求解,将每个线程计算的结果加到全局变量pi中。主函数创建多个线程并等待它们完成计算,最后输出最终的PI近似值。
阅读全文