线程同步打印5个随机数
Thread syncronization to print 5 random numbers
我被要求写一个程序,它将有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);
}
实际上我写它没有编译器,所以可能会有一些错误,但概念应该工作
相关文章:
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 如何对0,1,..中的k个随机数进行采样..,n-1,而不缓存到数组
- C++多个实例的随机数生成器组种子中增强
- C++:随机数列表:x 个随机整数
- C++ 简单的掷骰子 - 如何返回多个不同的随机数
- 如何在C++中生成 4 个不同的随机数
- 为什么这4个不同的随机数生成器函数会产生相同的一系列数字
- 如何生成多个随机数?
- 在random_device和seed_seq之间做出决定,为多个随机数序列生成种子
- 使用具有唯一随机数的结构数组创建多个对象
- C++生成多个随机数而不循环
- 如何在C 中生成4个不同的数字随机数
- 本应生成 2 个介于 1 和 9 之间的随机数的程序C++只生成 6 和 9 并且没有正确循环
- 使用多个线程生成随机数时的瓶颈
- 生成唯一的多个随机数
- C 11多个随机数引擎适配器
- 生成 9 个随机数而不重复
- 介于 2 个值之间的随机数
- 如何在我存储的两个变量之间生成随机数
- 如何在两个不同的数组中生成随机数?(C++)