Синхронизация потоков для печати 5 случайных чисел - PullRequest
2 голосов
/ 17 декабря 2011

Меня попросили написать программу, которая будет иметь 2 потока и напечатать 5 случайных целых чисел, так что первый поток сгенерирует число, второй напечатает его.Затем первый сгенерирует 2-е число, второй поток напечатает его ... и т. Д., Используя мьютекс.

Мой код теперь выполняет его за один цикл.Как я могу расширить его, чтобы заставить потоки оправдывать методы 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);
}

Ответы [ 4 ]

2 голосов
/ 17 декабря 2011

Используйте условные переменные для синхронизации двух потоков. Когда поток завершил свою работу, он сигнализирует другому потоку о том, что он проснулся, а затем переходит в режим ожидания, ожидая дополнительной работы. Вот как то так:

// 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);
}
1 голос
/ 19 декабря 2011
#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;
 }
0 голосов
/ 17 декабря 2011

Вы можете сделать это так:

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);
}

На самом деле я написал это без компилятора, поэтому могут быть некоторые ошибки, но концепция должна работать.

0 голосов
/ 17 декабря 2011

Мьютекс здесь недостаточен. Вам понадобится переменная условия, чтобы убедиться, что числа печатаются в правильном порядке. Какой-то псевдокод:

//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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...