2013-10-15 50 views
-1

我在學習C編程和多線程。我開始編寫一些基本的東西[如下所示],但我陷入了困境。有人能幫我一把嗎?C使用pthread.h編程多線程

program.c

#include <string.h> 
#include <stdio.h> 
#include <pthread.h> 
#define NUM_THREADS 4 

void *main_thread(void *threadID) { 
    long tid; 
    tid = (long)threadID; 
    printf("main thread #%ld!\n", tid); 
    pthread_exit(NULL); 

} 

void *first_thread(void *threadID) { 
    long tid; 
    tid = (long)threadID; 
    printf("first thread #%ld!\n", tid); 
    pthread_exit(NULL); 

} 

void *second_thread(void *threadID) { 
    long tid; 
    tid = (long)threadID; 
    printf("second thread #%ld!\n", tid); 
    pthread_exit(NULL); 

} 

void *last_thread(void *threadID) { 
    long tid; 
    tid = (long)threadID; 
    printf("last thread #%ld!\n", tid); 
    pthread_exit(NULL); 

} 



int main() { 
    pthread_t threads[NUM_THREADS]; 
    int rc; 
    long t; 

    for (t=0; t < NUM_THREADS; t++) { 
     printf("In main Function creating thread %ld\n", t); 
     rc = pthread_create(&threads[t], NULL, first_thread, (void *)t); 
     if (rc) { 
      printf("ERROR; Return code from pthread_create is %d\n", rc); 
      exit(-1); 
     } 

    } 


    pthread_exit(NULL); 
    return 0; 
} 

我會不斷更新上面我想新的東西出來 *嗨代碼。我沒有妥善編譯它,但現在我想通了。 GCC -pthread -o主要的main.c

+0

是不是爲任何基本的pthread編程讀過任何書籍或文檔? – tristan

+0

使用pthread_join等待main中的所有線程。 –

+0

我剛開始閱讀一些在線的東西。但它有點混亂。 – NewFile

回答

1

此代碼將通過在每個我們需要計算一個數的冪項計算其系列, 方面計算E 1 x的值(在本方案它爲x )和每個相應功率的階乘。

因爲這兩個計算是獨立的,所以我們爲這兩個函數做兩個線程,這兩個線程將並行運行。

在計算這兩個函數的值(這兩個線程的末尾)之後,我們需要合併結果(我的意思是功率/因子),然後將所有這些結果添加到另一個並行線程中,這些線程將在這兩個線程之後運行。

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

long double x,fact[150],pwr[150],s[1]; 
int i,term; 

void *Power(void *temp) 
{ 
    int k; 

    for(k=0;k<150;k++) 
    { 
     pwr[k] = pow(x,k); 
    //printf("%.2Lf\n",pwr[k]); 
    } 

    return pwr; 
} 

void *Fact(void *temp) 
{ 
    long double f; 
    int j; 

    fact[0] = 1.0; 

    for(term=1;term<150;term++) 
    { 
     f = 1.0; 

     for(j=term;j>0;j--) 
     f = f * j; 

     fact[term] = f; 
     //printf("%.2Lf\n",fact[term]); 
    } 

    return fact; 
} 

void *Exp(void *temp) 
{ 
    int t; 
    s[0] = 0; 

    for(t=0;t<150;t++) 
    s[0] = s[0] + (pwr[t]/fact[t]); 

    return s; 
} 

int main(void) 
{ 
    pthread_t thread1,thread2,thread3; 

    printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : "); 
    scanf("%Lf",&x); 

    printf("\nThreads creating.....\n"); 
    pthread_create(&thread1,NULL,Power,NULL); //calling power function 
    pthread_create(&thread2,NULL,Fact,NULL); //calling factorial function 
    printf("Threads created\n"); 

    pthread_join(thread1,NULL); 
    pthread_join(thread2,NULL); 
    printf("Master thread and terminated threads are joining\n"); 
    printf("Result collected in Master thread\n"); 

    pthread_create(&thread3,NULL,Exp,NULL); 
    pthread_join(thread3,NULL); 

    printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]); 

    exit(1); 
}