高性能计算实验三3 Pthreads 求解二次方程组的根

代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>


int done=0;
double a,b,c,d;
double x1,x2;

pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void thr_exit(){
	pthread_mutex_lock(&m);
	done++;
	if(done==2)
		pthread_cond_signal(&cond);
	pthread_mutex_unlock(&m);
}


void *child1_add(void *arg){
	printf("child1\n");
	if(d>=0)
		x1=(-b+d)/ (2 * a);
	thr_exit();
	return NULL;
}

void *child2_sub(void *arg){
	printf("child2\n");
	if(d>=0)
		x2=(-b-d)/ (2 * a);
	thr_exit();
	return NULL;
}

void thr_join(){
	pthread_mutex_lock(&m);
	while(done<2)
		pthread_cond_wait(&cond,&m);
	pthread_mutex_unlock(&m);
}

int main(int argc,char *argv[]){
	a= atoi(argv[1]); 
  	b= atoi(argv[2]); 
	c = atoi(argv[3]); 
	d = sqrt(b * b - 4 * a * c);
	printf("parent:begin\n");
	pthread_t p1,p2;
	pthread_create(&p1,NULL,child1_add,NULL);
	pthread_create(&p2,NULL,child2_sub,NULL);
	thr_join();
	if(d>=0)
		printf("x1=%.4f, x2=%.4f\n", x1, x2);
	else
		printf("no answer!");
	printf("parent:end\n");
	return 0;
}

 运行:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值