knoppix@Microknoppix:/mnt-system/lx/test$ gcc -D_REENTRANT -lpthread -o testcondout testcondout.c
knoppix@Microknoppix:/mnt-system/lx/test$ ./testcondout
please input an integer:(<=1000)26
time out
time out
1/1 finished,push 1.0000000000
1/2 finished,push 0.5000000000
1/3 finished,push 0.3333333333
1/4 finished,push 0.2500000000
1/5 finished,push 0.2000000000
time out
1/6 finished,push 0.1666666667
1/7 finished,push 0.1428571429
1/8 finished,push 0.1250000000
1/9 finished,push 0.1111111111
1/10 finished,push 0.1000000000
time out
1/11 finished,push 0.0909090909
1/12 finished,push 0.0833333333
1/13 finished,push 0.0769230769
1/14 finished,push 0.0714285714
1/15 finished,push 0.0666666667
1/1:1.0000000000 added result 1.0000000000
1/2:0.5000000000 added result 1.5000000000
1/3:0.3333333333 added result 1.8333333333
1/4:0.2500000000 added result 2.0833333333
1/5:0.2000000000 added result 2.2833333333
1/6:0.1666666667 added result 2.4500000000
1/7:0.1428571429 added result 2.5928571429
1/8:0.1250000000 added result 2.7178571429
1/9:0.1111111111 added result 2.8289682540
1/10:0.1000000000 added result 2.9289682540
1/11:0.0909090909 added result 3.0198773449
1/12:0.0833333333 added result 3.1032106782
1/13:0.0769230769 added result 3.1801337551
1/14:0.0714285714 added result 3.2515623266
1/15:0.0666666667 added result 3.3182289932
*******1/1:1.0000000000 computed result 1.0000000000
*******1/2:0.5000000000 computed result 0.5000000000
*******1/3:0.3333333333 computed result 0.8333333333
*******1/4:0.2500000000 computed result 0.5833333333
*******1/5:0.2000000000 computed result 0.7833333333
*******1/6:0.1666666667 computed result 0.6166666667
*******1/7:0.1428571429 computed result 0.7595238095
*******1/8:0.1250000000 computed result 0.6345238095
*******1/9:0.1111111111 computed result 0.7456349206
*******1/10:0.1000000000 computed result 0.6456349206
*******1/11:0.0909090909 computed result 0.7365440115
*******1/12:0.0833333333 computed result 0.6532106782
*******1/13:0.0769230769 computed result 0.7301337551
*******1/14:0.0714285714 computed result 0.6587051837
*******1/15:0.0666666667 computed result 0.7253718504
===============compute finish!=========== result:0.7253718504
1/16 finished,push 0.0625000000
1/17 finished,push 0.0588235294
1/18 finished,push 0.0555555556
1/19 finished,push 0.0526315789
1/20 finished,push 0.0500000000
1/16:0.0625000000 added result 3.3807289932
1/17:0.0588235294 added result 3.4395525226
1/18:0.0555555556 added result 3.4951080782
1/19:0.0526315789 added result 3.5477396571
1/20:0.0500000000 added result 3.5977396571
1/21 finished,push 0.0476190476
1/22 finished,push 0.0454545455
1/23 finished,push 0.0434782609
1/24 finished,push 0.0416666667
1/25 finished,push 0.0400000000
time out
*******1/16:0.0625000000 computed result 0.6628718504
*******1/17:0.0588235294 computed result 0.7216953798
*******1/18:0.0555555556 computed result 0.6661398242
*******1/19:0.0526315789 computed result 0.7187714032
*******1/20:0.0500000000 computed result 0.6687714032
*******1/21:0.0476190476 computed result 0.7163904508
*******1/22:0.0454545455 computed result 0.6709359053
*******1/23:0.0434782609 computed result 0.7144141662
*******1/24:0.0416666667 computed result 0.6727474995
*******1/25:0.0400000000 computed result 0.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
2个线程完成累加和累加减运算(其中一个采用超时等待条件信号,另一个采用等待条件信号),n个线程完成计算每个符点数
#include <pthread.h>
#include <bits/pthreadtypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#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<max){
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 timeval 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;
double jg=0;
int fh=1;
int rc;
while(curi<=max)
{
maxi=0;
sleep(2);
pthread_mutex_lock(&eventlock);//用于条件变量的互斥,条件变量就是一个用来发送事件发生信号的信号量
rc=pthread_cond_wait(&myevent,&eventlock);//在等待条件变量myevent的发生。条件变量必须与一个互斥锁eventlock相关联,条件变量不提供锁定,必须有一个互斥锁eventlock配合。
//互斥锁eventlock在调用wait前应锁定,然后在wait期间,互斥量eventlock被解锁。挂起线程执行,直到条件变量myevent收到信号
// myhaspl
if (rc==0){
maxi=myjg[0];
fflush(stdout);
pthread_mutex_unlock(&eventlock);
while (curi<=maxi){
jg+=fh*myjg[curi];
printf("*******1/%d:%.10f computed result %.10f\n",curi,myjg[curi],jg);
fflush(stdout);
fh=-fh;
curi++;
}
printf("===============compute finish!=========== result:%.10f\n",jg);//输出累加结果
fflush(stdout);
}
else{//error
perror("pthread_cond_wait");
fflush(stdout);
pthread_mutex_unlock(&eventlock);
continue;
}
}
pthread_exit(NULL);
}
int main(){
//计算1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......
pthread_mutex_init(&eventlock,NULL);
pthread_cond_init(&myevent,NULL);
int i =0;
printf("please input an integer:(<=%d)",MAXS);
while (scanf("%d",&max),max>MAXS){//n的最大值
printf("please input an integer:(<=%d)",MAXS);
};
//myhaspl
myjg[0]=0;
pthread_create(&(threads[i]),NULL,myprint1,(void *)&i);
sleep(1);
i++;
pthread_create(&(threads[i]),NULL,myprint2,(void *)&i);
sleep(1);
i++;
for (;i<=MAXTDS;i++){
pthread_create(&(threads[i]),NULL,mycomp,(void *)&i);
sleep(1);
}
sleep(MAXTDS*2*(i/10+1)); //wait......
pthread_mutex_destroy(&eventlock);
return(0);
}