#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <math.h> #include <sys/time.h> #define NUM_THREADS 4 #define TOTAL_POINTS 10000000 #define REPORT_INTERVAL 1000 pthread_mutex_t mutex; int total_points_in_circle = 0; int total_points_generated = 0; void* generate_points(void* arg) { int points_in_circle = 0; struct timeval tv; gettimeofday(&tv, NULL); unsigned int seed = tv.tv_sec ^ tv.tv_usec ^ pthread_self(); while (1) { pthread_mutex_lock(&mutex); if (total_points_generated >= TOTAL_POINTS) { pthread_mutex_unlock(&mutex); break; } total_points_generated++; pthread_mutex_unlock(&mutex); double x = (double)rand_r(&seed) / RAND_MAX * 2 - 1; double y = (double)rand_r(&seed) / RAND_MAX * 2 - 1; if (sqrt(x*x + y*y) <= 1) { points_in_circle++; } if (total_points_generated % REPORT_INTERVAL == 0) { pthread_mutex_lock(&mutex); total_points_in_circle += points_in_circle; printf("Points: (%d,%d)\n", x,y); pthread_mutex_unlock(&mutex); points_in_circle = 0; } } pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int i; struct timeval start_time, end_time; pthread_mutex_init(&mutex, NULL); gettimeofday(&start_time, NULL); // 获取程序开始时间 for (i = 0; i < NUM_THREADS; i++) { pthread_create(&threads[i], NULL, generate_points, NULL); } for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } gettimeofday(&end_time, NULL); // 获取程序结束时间 pthread_mutex_destroy(&mutex); double pi = 4.0 * total_points_in_circle / TOTAL_POINTS; printf("Estimated value of pi: %lf\n", pi); // 计算程序运行时间 double execution_time = (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec) / 1000000.0; printf("Execution time: %lf seconds\n", execution_time); return 0; }给这段程序每一句后加上注释
时间: 2024-04-26 17:23:27 浏览: 216
// 引入需要的头文件
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <sys/time.h>
// 定义常量
#define NUM_THREADS 4 // 线程数
#define TOTAL_POINTS 10000000 // 总的点数
#define REPORT_INTERVAL 1000 // 报告进度的间隔
// 定义全局变量和互斥锁
pthread_mutex_t mutex; // 互斥锁
int total_points_in_circle = 0; // 圆内的点数
int total_points_generated = 0; // 已经生成的点数
// 线程函数,用于生成随机点
void* generate_points(void* arg) {
int points_in_circle = 0; // 本线程生成的圆内的点数
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned int seed = tv.tv_sec ^ tv.tv_usec ^ pthread_self(); // 随机种子,使用线程ID和时间戳异或生成
// 无限循环,直到生成了足够的点数
while (1) {
pthread_mutex_lock(&mutex); // 加锁
if (total_points_generated >= TOTAL_POINTS) { // 如果已经生成了足够的点数
pthread_mutex_unlock(&mutex); // 解锁
break; // 退出循环
}
total_points_generated++; // 总的已生成点数加1
pthread_mutex_unlock(&mutex); // 解锁
// 生成随机点
double x = (double)rand_r(&seed) / RAND_MAX * 2 - 1;
double y = (double)rand_r(&seed) / RAND_MAX * 2 - 1;
// 判断该点是否在圆内
if (sqrt(x*x + y*y) <= 1) {
points_in_circle++; // 圆内点数加1
}
// 每隔一段时间报告当前进度
if (total_points_generated % REPORT_INTERVAL == 0) {
pthread_mutex_lock(&mutex); // 加锁
total_points_in_circle += points_in_circle; // 更新总的圆内点数
printf("Points: (%d,%d)\n", x,y); // 输出当前点的坐标
pthread_mutex_unlock(&mutex); // 解锁
points_in_circle = 0; // 重置本线程圆内点数
}
}
pthread_exit(NULL); // 退出线程
}
int main() {
pthread_t threads[NUM_THREADS]; // 线程数组
int i;
struct timeval start_time, end_time;
pthread_mutex_init(&mutex, NULL); // 初始化互斥锁
gettimeofday(&start_time, NULL); // 获取程序开始时间
// 创建多个线程
for (i = 0; i < NUM_THREADS; i++) {
pthread_create(&threads[i], NULL, generate_points, NULL);
}
// 等待所有线程结束
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
gettimeofday(&end_time, NULL); // 获取程序结束时间
pthread_mutex_destroy(&mutex); // 销毁互斥锁
// 计算估算的圆周率值
double pi = 4.0 * total_points_in_circle / TOTAL_POINTS;
printf("Estimated value of pi: %lf\n", pi);
// 计算程序运行时间
double execution_time = (end_time.tv_sec - start_time.tv_sec) +
(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
printf("Execution time: %lf seconds\n", execution_time);
return 0;
}
阅读全文