程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C指針原理(89)-LINUX應用(2)-線程池

C指針原理(89)-LINUX應用(2)-線程池

編輯:關於C語言

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

麥好的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 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);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved