线程同步打印5个随机数

Thread syncronization to print 5 random numbers

本文关键字:随机数 5个 打印 同步 线程      更新时间:2023-10-16

我被要求写一个程序,它将有2个线程并打印5个随机整数,这样第一个线程将生成一个数字,第二个线程将打印它。然后第一个线程将生成第二个数字,第二个线程将打印它…

我的代码现在执行它一个周期。我如何扩展它,使线程执行方法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);
}

使用条件变量同步两个线程。当一个线程完成它的工作时,它向另一个线程发出唤醒信号,然后进入睡眠状态,等待更多的工作。像这样:

// 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 %dn", genNumber);
    signal(c1);
    unlock(mutex);
}
#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 %dn",gennumber);
            i++;
            pthread_mutex_unlock(&m);

    }
    return 0;
 }

一个互斥锁在这里是不够的。您将需要一个条件变量来确保以正确的顺序打印数字。一些伪代码:

//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
}

你可以这样做:

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

实际上我写它没有编译器,所以可能会有一些错误,但概念应该工作