.7127474995
===============compute finish!=========== result:0.7127474995
1/21:0.0476190476 added result 3.6453587048
1/22:0.0454545455 added result 3.6908132502
1/23:0.0434782609 added result 3.7342915111
1/24:0.0416666667 added result 3.7759581778
1/25:0.0400000000 added result 3.8159581778
1/26 finished,push 0.0384615385
time out
time out
*******1/26:0.0384615385 computed result 0.6742859611
===============compute finish!=========== result:0.6742859611
1/26:0.0384615385 added result 3.8544197162
================add finish!============ result:3.8544197162
麦好的AI乐园博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
2个线程完成累加和累加减运算(其中一个采用超时等待条件信号,另一个采用等待条件信号),n个线程完成计算每个符点数
#include
#include
#include
#include
#include
#define MAXS 1000
#define MAXTDS 5 //线程池大小
double myjg[MAXS+1];//计算结果存放位置
int max;
pthread_mutex_t eventlock; //互斥锁
pthread_cond_t myevent; //条件变量
pthread_t threads[MAXTDS+2]; //线程池,完成1/n计算
int isend=0;
int done;
void *mycomp(void *x){//计算1/i的结果,计算结果放在一个数组中。
int i=0;
int rc;
while (1){
pthread_mutex_lock(&eventlock);
if (isend){
pthread_mutex_unlock(&eventlock);
break;
}
i=myjg[0];//myjg[0]存放着线程已经计算到的i。
if (i
i++;
myjg[0]=i;
}
if (i==max){//最后一个数
myjg[i]=(1/(double)i);
isend=1;
printf("1/%d finished,push %.10f\n",i,myjg[i]);
fflush(stdout);
pthread_mutex_unlock(&eventlock);
sleep(3);
rc=pthread_cond_signal(&myevent);//广播信号,多个任务不被阻塞,多个任务竞争互斥锁的所有权。也可以使用pthread_cond_signal(&event);发送信号,这样只有一个线程不被阻塞,其它线程都被阻塞。
if (rc){
perror("pthread_cond_broadcast");
fflush(stdout);
}
sleep(2);
break;
}
//开始计算
myjg[i]=(1/(double)i);
printf("1/%d finished,push %.10f\n",i,myjg[i]);
fflush(stdout);
pthread_mutex_unlock(&eventlock);
if (!(i%MAXTDS)){
sleep(3);
pthread_cond_broadcast(&myevent);//广播信号,多个任务不被阻塞,多个任务竞争互斥锁的所有权。也可以使用pthread_cond_signal(&event);发送信号,这样只有一个线程不被阻塞,其它线程都被阻塞。
sleep(3);
}
}
pthread_exit(NULL);
}
void *myprint1(void *xx){//读取数组,将计算结果累加,最终完成1/1+1/2+1/3+......+1/n的计算,使用超时等待
int maxi;
int curj=1;
double jg=0;
int rc;
struct timeva l now;//使用微秒
struct timespec timeout; //使用纳秒
while(curj<=max)
{
//取当前时间
// myhaspl
gettimeofday(&now);
//准备时间间隔
timeout.tv_sec=now.tv_sec+1;
timeout.tv_nsec=now.tv_usec*1000;
maxi=0;
pthread_mutex_lock(&eventlock);//用于条件变量的互斥,条件变量就是一个用来发送事件发生信号的信号量
rc=pthread_cond_timedwait(&myevent,&eventlock,&timeout);//在等待条件变量myevent的发生,超时就返回,不再等待。条件变量必须与一个互斥锁eventlock相关联,条件变量不提供锁定,必须有一个互斥锁eventlock配合。
//互斥锁eventlock在调用wait前应锁定,然后在wait期间,互斥量eventlock被解锁。挂起线程执行,直到条件变量myevent收到信号
if (rc==0){ // myhaspl
maxi=myjg[0];
fflush(stdout);
pthread_mutex_unlock(&eventlock);
for (;curj<=maxi;curj++)
{ // myhaspl
jg+=myjg[curj];
printf("1/%d:%.10f added result %.10f\n",curj,myjg[curj],jg);
fflush(stdout);
}
}
else if (rc==ETIMEDOUT){//TIMEOUT
printf("time out\n");
fflush(stdout);
pthread_mutex_unlock(&eventlock);
continue;
}
else { // myhaspl
perror("pthread_cond_wait");
fflush(stdout);
pthread_mutex_unlock(&eventlock);
continue;
}
}
printf("================add finish!============ result:%.10f\n",jg);//输出累加结果。
fflush(stdout);
pthread_exit(NULL);
}
void *myprint2(void *xx){//读取数组,将计算结果完成1/1+1/2-1/3+1/4-1/5......的计算
int maxi=0;
int curi=1;
dou