linux多进程编程计算圆周率,中值积分定理计算pi值的多线程实现 -尊龙游戏旗舰厅官网
// parallel.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
static long num_steps = 100000;
const int numthreads = 4;
double step, pi;
critical_section g_cs;
double sum = 0.0;
dword winapi countfunc(lpvoid parg) {
double x;
int i;
int temp = *(int *)parg;
int start = (temp*num_steps) / 4;
int end = start num_steps / 4;
for (i = start; i < end; i ) {
entercriticalsection(&g_cs);
x = (i 0.5)*step;
sum = sum 4.0 / (1.0 x*x);
leavecriticalsection(&g_cs);
}
return 0;
}
void main()
{
int i;
handle hthread[numthreads];
step = 1.0 / (double)num_steps;
int tnum[numthreads];
initializecriticalsection(&g_cs);
for (int i = 0; i < numthreads; i ) {
tnum[i] = i;
hthread[i] = createthread(null, 0, countfunc, (lpvoid)&tnum[i], 0, null);
}
waitformultipleobjects(numthreads, hthread, true, infinite);
deletecriticalsection(&g_cs);
pi = step * sum;
printf("pi = .9f\n", pi);
system("pause");
}
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是尊龙游戏旗舰厅官网为你收集整理的linux多进程编程计算圆周率,中值积分定理计算pi值的多线程实现的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: linux 内核修改rss,linux
- 下一篇: linux修复uefi启动文件,uefi