2017-07-25 66 views
0

我有一個程序與4個線程,最後我應該打印線程和管道(2個週期)階段。喜歡的東西:如何讓4個線程互相交互?

Thread 2: Stage 1 and 2 
Thread 3: Stage 2 and 3 
Thread 1: Stage 3 and 4 
Thread 4: Stage 4 and 5 

但我不知道如何做到這一點櫃檯階段,因爲我在做什麼,我不能證明什麼,但1級和2爲每個線程,而不是1和2 ,2和3 ...

#include <stdio.h> 
#include <pthread.h> 
#include <time.h> 
#include <stdlib.h> 
#include <unistd.h> 

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 

void delay (int miliseconds){ 

    long pause; 
    clock_t now,then; 

    pause = miliseconds*(CLOCKS_PER_SEC/1000); 
    now = then = clock(); 
    while((now-then) < pause) 
     now = clock(); 
} 

int print(int n, int parar){ 

    if (n == 5) { 
     printf("\nEstágio 4 e 5"); 

    } else { 
     printf("Estágio %i e %i\n",n, n+1); 
     if (parar == 2) { 
      return (n+1, parar+0); 
     } 

     return print(n+1, parar+1); 
    } 
} 

void thread_cont(void *arg){ 

    int *pvalor; 
    pvalor=arg; 

    pthread_mutex_lock(&mutex); 
    printf ("\n---Thread %i--- \n", *pvalor); 
    int a = print(1, 1); 
    delay(2000); 
    pthread_mutex_unlock(&mutex); 

/* 
    if (*pvalor == 1){ 
     printf ("Estágio 1 e 2"); 
    } 

    if (*pvalor == 2){ 
    printf ("Estágio 2 e 3"); 
    } 

    if (*pvalor == 3){ 
    printf ("Estágio 3 e 4"); 
    } 

    if (*pvalor == 4){ 
    printf ("Estágio 4 e 5"); 
    } 

*/ 

} 

int main() { 

    pthread_t id1; 
    int offset1 = 1; 
    pthread_create(&id1, NULL, thread_cont, &offset1); 

    pthread_t id2; 
    int offset2 = 2; 
    pthread_create(&id2, NULL, thread_cont, &offset2); 

    pthread_t id3; 
    int offset3 = 3; 
    pthread_create(&id3, NULL, thread_cont, &offset3); 

    pthread_t id4; 
    int offset4 = 4; 
    pthread_create(&id4, NULL, thread_cont, &offset4); 

    pthread_join(id1, NULL); 
    pthread_join(id2, NULL); 
    pthread_join(id3, NULL); 
    pthread_join(id4, NULL); 

    printf("\n"); 

    return 0; 
} 
+0

因爲每個線程調用'打印(1,1)',它是很難理解爲什麼你期望不同的行爲。也許你應該使用'print(* pvalor,* pvalor + 1)'?我不清楚'print()'函數真的應該做什麼,以及它爲什麼遞歸地調用它自己。另外,'return(n + 1,parar + 0);'和'return parar沒有什麼不同;'你也打算在那裏調用'print()'嗎?爲什麼'+ 0'? –

回答

0

我想我對我的問題做了錯誤的解釋。對不起,但是我想我現在就結束了。 4個線程。 4個指令,管道(也許),並沒有使用互斥量,但信號量。順便說一句,語言障礙已經變得更加困難,甚至在這裏問。

@edit最後,也許正確的代碼

#include <stdio.h> 
#include <pthread.h> 
#include <time.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <semaphore.h> 

sem_t mutex; 
pthread_t id1; 
pthread_t id2; 
pthread_t id3; 
pthread_t id4; 

void delay (int miliseconds){ 

    long pause; 
    clock_t now,then; 

    pause = miliseconds*(CLOCKS_PER_SEC/1000); 
    now = then = clock(); 
    while((now-then) < pause) 
     now = clock(); 
} 

pthread_mutex_t lock; 

void *thread_cont(void *arg){ 

    int *pvalor; 
    pvalor=arg; 


    pthread_mutex_lock(&lock); 
    sem_wait(&mutex); 
    printf ("\n---Thread %i---", *pvalor); 
    printf ("Stage 1"); 
    delay(2000); 


    printf ("\n---Thread %i---", *pvalor); 
    printf ("Stage 2"); 
    sem_post(&mutex); 
    pthread_mutex_unlock(&lock); 


    printf ("\n---Thread %i---", *pvalor); 
    printf ("Stage 3"); 
    delay(2000); 

    printf ("\n---Thread %i---", *pvalor); 
    printf ("Stage 4"); 
    delay(2000); 

    pthread_exit(0); 

} 


int main(){ 

    //create threads 

    sem_init(&mutex, 0, 2); 


//ONE FOR EACH CREATE 

int offset1 = 1; 
int offset2 = 2; 
int offset3 = 3; 
int offset4 = 4; 


    pthread_create(&id1, NULL, thread_cont, &offset1);  
    delay(2000); 
    pthread_create(&id2, NULL, thread_cont, &offset2); 
    delay(2000); 
    pthread_create(&id3, NULL, thread_cont, &offset3); 
    delay(2000); 
    pthread_create(&id3, NULL, thread_cont, &offset3); 
    delay(2000); 
    pthread_create(&id4, NULL, thread_cont, &offset4); 
    delay(2000); 


    pthread_join(id1, NULL); 
    pthread_join(id2, NULL); 
    pthread_join(id3, NULL); 
    pthread_join(id4, NULL); 


    sem_destroy(&mutex); 

    printf("\n"); 

    return 0; 
}