2011-12-17 40 views
2

2つのスレッドを持つプログラムを作成し、5つの乱数を出力して、最初のスレッドが数値を生成し、2番目のスレッドが数値を生成するように求めます。次に、最初の2番目の番号を生成し、2番目のスレッドはそれを印刷します...などmutexを使用しています。5つの乱数を印刷するためのスレッド同期化

これで、私のコードで1サイクル実行されます。どのようにスレッドをメソッドを5回実行させるためにそれを拡張できますか?

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

void* generate (void*); 
void* print (void*); 

pthread_mutex_t m; 
int number = 5; 
int genNumber; 


int main() 
{ 
    int i; 
    srandom(getpid()); 
    pthread_t th[2]; 

    pthread_mutex_init(&m,NULL); 

    pthread_create(&th[0],NULL,generate,NULL); 
    pthread_create(&th[1],NULL,print, NULL); 

    for (i = 0; i < 2; i++) 
     pthread_join(th[i], NULL); 

    pthread_mutex_destroy(&m); 

    return 0; 
} 

void* generate(void* arg) 
{ 
    pthread_mutex_lock(&m); 
    genNumber = random() % 9; 
    printf("Generated #1 \n"); 
    pthread_mutex_unlock(&m); 
} 

void* print(void* arg) 
{ 
    pthread_mutex_lock(&m); 
    printf("The number is %d " , genNumber); 
    pthread_mutex_unlock(&m); 
    pthread_exit(NULL); 
} 
+0

なぜこのためにマルチスレッドを使用していますか?宿題の要件ですか? –

+0

はい。 mutexとセマフォの練習として –

答えて

2

condition variablesを使用して2つのスレッドを同期させます。スレッドは作業を完了すると、別のスレッドに信号を送り、スリープ状態に移行してより多くの作業を待つ。したがって、このような何か:

// Pseudocode 
pthread_cond_t c1, c2; 
pthread_mutex_t mutex; 

// Thread 1 (producer): 
for(int i = 0; i < 5; i++) 
{ 
    lock(mutex); 
    genNumber = random() % 9; 
    signal(c2); 
    wait(c1, mutex); 
    unlock(mutex); 
} 

// Thread 2 (consumer): 
for(int i = 0; i < 5; i++) 
{ 
    lock(mutex); 
    wait(c2, mutex); 
    print("The number is %d\n", genNumber); 
    signal(c1); 
    unlock(mutex); 
} 
+0

ロック(mutex)の前にプロデューサーが最初にロックを解除(ミューテックス)して待機(c1、ミューテックス)し、コンシューマーを待つ(c2、ミューテックス)しないでください。 – Ulterior

+0

@Ulterior: 'pthread_cond_wait()'はmutexを解放します。 – ninjalj

+0

@ninjaljありがとう、私はこれについてもっと読む必要があります... – Ulterior

0

mutexはここでは十分ではありません。数値が正しい順序で印刷されるように、条件変数が必要です。いくつかの擬似コード:

//producer thread: 
for(int i = 0; i < 5; i++) 
{ 
    number = random(); 
    signal the other thread with pthread_cond_signal 
    wait for signal from the consumer 
} 

// consumer thread 
for(int i = 0; i < 5; i++) 
{ 
    wait for signal with pthread_cond_wait 
    print number 
    signal the producer to produce another number 
} 
+0

あなたは1つのミューテックスとポーリングでこれを行うことができます。しかし明白にそうでなければもっと良い – Voo

0

あなたはこのようにそれを行うことができます。

int* generated = null; 

void generate() { 
    int i = 0; 
    while (i<5) { 
    pthread_mutex_lock(&m); 
    if (generated == null) { 
     generated = malloc(int); 
     *generated = random() % 9; 
     printf("Generated #1 \n"); 
     ++i; 
    } 
    pthread_mutex_unlock(&m); 
    } 
    pthread_exit(NULL); 
} 

void print() { 
    int i = 0; 
    while (i<5) { 
    pthread_mutex_lock(&m); 
    if (generated != null) { 
     printf("The number is %d " , generated); 
     free(generated); 
     generated=null; 
    } 
    pthread_mutex_unlock(&m); 
    } 
    pthread_exit(NULL); 
} 

実際に多少の誤差があることができますので、私は、コンパイラなしでそれを書いたのが、コンセプトは動作するはずです。

1
#include<stdio.h> 
#include<stdlib.h> 
#include<pthread.h> 
#include<unistd.h> 
static int *generate(void *); 
static int *print(void *); 
pthread_mutex_t m; 
pthread_cond_t con; 
int munmber=10; 
int gennumber; 

int main() { 
    srandom(getpid()); 
    pthread_t th1,th2; 
    pthread_mutex_init(&m,NULL); 
    pthread_create(&th2,NULL,print,NULL); 
    sleep(1); 
    pthread_create(&th1,NULL,generate,NULL); 
    pthread_join(th1,NULL); 
    pthread_join(th2,NULL); 
    pthread_mutex_destroy(&m); 

    } 

    static int *generate(void *arg) { 
    int i; 
    while(i<5) { 
      pthread_mutex_lock(&m); 
      gennumber=random()%8; 
      printf("NUMMBER GENERATED.... \n"); 
      pthread_cond_signal(&cond); 
      i++; 
      pthread_mutex_unlock(&m); 
      sleep(2); 
      if(i==5) 
       exit(1); 
    } 

    return 0; 
    } 
    static int *print(void *arg) { 
    int i; 
    while('a') { 
      pthread_cond_wait(&cond,&m); 
      printf("GENERATED NUMBER is %d\n",gennumber); 
      i++; 
      pthread_mutex_unlock(&m); 


    } 

    return 0; 
} 
関連する問題