所以我正在为使用基本GNU随机数生成器的赋值创建一个数字生成器。理想情况下,我需要在现阶段开发
On loop 0, just produced...1804289383
On loop 0, I just consumed...1804289383.
等等,但是现在,我的输出是:
On loop 0, just produced...1804289383
On loop 1, just produced...846930886
On loop 2, just produced...1681692777
On loop 3, just produced...1714636915
On loop 4, just produced...1957747793
On loop 5, just produced...424238335
On loop 6, just produced...719885386
On loop 7, just produced...1649760492
On loop 8, just produced...596516649
On loop 9, just produced...1189641421
On loop 0, I just consumed...1804289383
On loop 1, I just consumed...846930886
On loop 2, I just consumed...1681692777
On loop 3, I just consumed...1714636915
On loop 4, I just consumed...1957747793
On loop 5, I just consumed...424238335
On loop 6, I just consumed...719885386
On loop 7, I just consumed...1649760492
On loop 8, I just consumed...596516649
On loop 9, I just consumed...1189641421
直到它制作了一堆数字之后才会打击消费者。
这里发生了什么?我一直在将它与另一段执行类似功能的代码进行比较,这就是我需要做的事情,而且这两段代码非常相似。我只需要第二眼,也许我错过了一些简单的东西?
我的代码:
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_LOOP 10 //how many times the loop is allowed to go
#define MAX_VALUE 10 //how hig the random number can be
#define TRUE 1 //boolean value
#define FALSE 0 // ""
#define BUFFER_SIZE 10
#define DEBUG TRUE //debug variable. if on 1, it checks for bugs and stuff
pthread_cond_t cond; // conditional variable global
pthread_mutex_t the_mutex;
int bufferRandomFirst[BUFFER_SIZE]; //shared buffer array
int numOfItemsInQueue = 0;//number of items in the array
void *producerRandom(void *ptr)
{
/*
*
* Random() Producer function:
* Creates numbers for the consumer to process using gcc random()
*
*/
int i = 0;
int rand_num = 0;
int rear = 0; //beginning of queue
srandom((unsigned int)0) ; //make some random seeds
for (i = 0; i< BUFFER_SIZE; i++){
rand_num =random(); //make a random number
pthread_mutex_lock(&the_mutex); //get access to the buffer
while (numOfItemsInQueue ==BUFFER_SIZE) {
pthread_cond_wait(&cond, &the_mutex); //if the buffer is not empty, producer must stop
}//end of while
bufferRandomFirst[rear] = rand_num; //store the number in the buffer
if (DEBUG){
printf("On loop %d, just produced...%d\n", i, bufferRandomFirst[rear]);
}
rear = (rear + 1) % BUFFER_SIZE; //does the ciruclar queue loop
numOfItemsInQueue +=1;
pthread_cond_signal(&cond); //fires up the consumer process
pthread_mutex_unlock(&the_mutex); // unlocks the mutex
}//end of for loop
pthread_exit(0); //exit the thread
}//end producer
void *consumerRandom(void *ptr)
{
/*
*
* Random() Consumer Function
* consumes the numbers that the producer makes
*
*/
int front = 0; //front of the queue
int lastDigit = 0; //last digit of random number (thanks Nathan)
int i = 0; //counting variable for for loops
for (i = 0; i < BUFFER_SIZE; i++){
pthread_mutex_lock(&the_mutex); //get exclusive access to buffer
while(numOfItemsInQueue == 0){
pthread_cond_wait(&cond, &the_mutex);
}//end of while
if (DEBUG){
printf("On loop %d, I just consumed...%d\n", i, bufferRandomFirst[i]);
}
lastDigit = bufferRandomFirst[front] % 10; //steals the last digit from the random number
//for example, if the number was 100, 100 mod 10
//is 0, so you get 0. if it was 101, you would get 1
front = (front + 1) % BUFFER_SIZE; //wrap around the array
numOfItemsInQueue -= 1;
pthread_cond_signal(&cond);//wake up producer
pthread_mutex_unlock(&the_mutex);
}//end of for loop
pthread_exit(0); //exit the loop
}//end of consumer
int main(int argc){
pthread_t thread_one, thread_two; //create two threads for Random() Function
printf("Random GNU numbers inbound!\n");
//Random GCC pthread handing/
pthread_mutex_init(&the_mutex,0); //set up the mutex
pthread_cond_init(&cond, 0); //set up the condition
//makes a thread
pthread_create(&thread_one, 0, producerRandom, 0);
//makes a thread
pthread_create(&thread_two, 0, consumerRandom, 0);
pthread_join(thread_one, 0); //wait on this thread
pthread_join(thread_two, 0); //wait on this one too. =)
pthread_cond_destroy(&cond); //condition destroy
pthread_mutex_destroy(&the_mutex); //destroy the mutex resources
printf("And that is all for those numbers\n\n");
}//end of main
答案 0 :(得分:1)
从我最初浏览您的源代码开始,生产者和消费者之间没有任何联锁会导致您期望的行为。例如,在生产者中,没有任何东西可以告诉它在产生另一个项目之前等待一个项目被消耗。 the_mutex
在生产者循环结束时释放,但会立即在循环顶部重新获取。如果OS调度程序决定您的生产者线程仍然可以继续运行,那么将生成另一个项目。
答案 1 :(得分:0)
查看最新更改。在发信号之前解锁the_mutex。产品/消费序列似乎不太确定。将printf语句移到临界区之外。在检查numOfItemsInQueue值时使用if语句而不是while循环。
// Compile with "gcc -pthread" command
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_LOOP 300 //how many times the loop is allowed to go
#define MAX_VALUE 10 //how big the random number can be
#define TRUE 1 //boolean value
#define FALSE 0 // ""
#define BUFFER_SIZE 20
#define DEBUG TRUE //debug variable. if on 1, it checks for bugs and stuff
pthread_cond_t cond; // conditional variable global
pthread_mutex_t the_mutex;
int bufferRandomFirst[BUFFER_SIZE]; //shared buffer array
int numOfItemsInQueue = 0;//number of items in the array
void *producerRandom(void *ptr)
{
/*
*
* Random() Producer function:
* Creates numbers for the consumer to process using gcc random()
*
*/
int i = 0;
int rand_num = 0;
int rear = 0; //beginning of queue
srandom((unsigned int)0) ; //make some random seeds
for (i = 0; i< MAX_LOOP; i++){
int rand_num =random(); //make a random number
pthread_mutex_lock(&the_mutex); //get access to the buffer
if (numOfItemsInQueue == BUFFER_SIZE) { // No need to loop: Upon pthread_cond_wait, we know for a fact that numOfItemsInQueue < BUFFER_SIZE (we have a single producer here)
pthread_cond_wait(&cond, &the_mutex); //if the buffer is full, producer must stop
}
bufferRandomFirst[rear] = rand_num; //store the number in the buffer
rear = (rear + 1) % BUFFER_SIZE; //does the circular queue loop
numOfItemsInQueue +=1;
pthread_mutex_unlock(&the_mutex); // unlocks the mutex
pthread_cond_signal(&cond); //fires up the consumer process
if (DEBUG){
printf("On loop %d, just produced...%d\n", i, rand_num);
}
}//end of for loop
pthread_exit(0); //exit the thread
}//end producer
void *consumerRandom(void *ptr)
{
/*
*
* Random() Consumer Function
* consumes the numbers that the producer makes
*
*/
int front = 0; //front of the queue
int i = 0; //counting variable for for loops
for (i = 0; i < MAX_LOOP; i++){
pthread_mutex_lock(&the_mutex); //get exclusive access to buffer
if(numOfItemsInQueue == 0){
pthread_cond_wait(&cond, &the_mutex); // If buffer is empty, consumer must stop
}
int rand_num = bufferRandomFirst[front];
front = (front + 1) % BUFFER_SIZE; //wrap around the array
numOfItemsInQueue -= 1;
pthread_mutex_unlock(&the_mutex);
pthread_cond_signal(&cond);//wake up producer
if (DEBUG){
printf("On loop %d, I just consumed...%d\n", i, rand_num);
}
}//end of for loop
pthread_exit(0); //exit the loop
}//end of consumer
int main(){
pthread_t thread_one, thread_two; //create two threads for Random() Function
printf("Random GNU numbers inbound!\n");
//Random GCC pthread handing/
pthread_mutex_init(&the_mutex,0); //set up the mutex
pthread_cond_init(&cond, 0); //set up the condition
//makes a thread
pthread_create(&thread_one, 0, producerRandom, 0);
//makes a thread
pthread_create(&thread_two, 0, consumerRandom, 0);
pthread_join(thread_one, 0); //wait on this thread
pthread_join(thread_two, 0); //wait on this one too. =)
pthread_cond_destroy(&cond); //condition destroy
pthread_mutex_destroy(&the_mutex); //destroy the mutex resources
printf("And that is all for those numbers\n\n");
}//end of main
缓冲区大小为5且MAX_LOOP = 30的输出。请注意开头
Random GNU numbers inbound!
On loop 0, just produced...1804289383
On loop 1, just produced...846930886
On loop 2, just produced...1681692777
On loop 3, just produced...1714636915
On loop 4, just produced...1957747793
On loop 0, I just consumed...1804289383
On loop 1, I just consumed...846930886
On loop 5, just produced...424238335
On loop 6, just produced...719885386
On loop 2, I just consumed...1681692777
On loop 3, I just consumed...1714636915
On loop 4, I just consumed...1957747793
On loop 5, I just consumed...424238335
On loop 6, I just consumed...719885386
On loop 7, just produced...1649760492
On loop 8, just produced...596516649
On loop 9, just produced...1189641421
On loop 10, just produced...1025202362
On loop 11, just produced...1350490027
On loop 7, I just consumed...1649760492
On loop 8, I just consumed...596516649
On loop 9, I just consumed...1189641421
On loop 12, just produced...783368690
On loop 13, just produced...1102520059
On loop 14, just produced...2044897763
On loop 10, I just consumed...1025202362
On loop 11, I just consumed...1350490027
On loop 12, I just consumed...783368690
On loop 13, I just consumed...1102520059
On loop 15, just produced...1967513926
On loop 16, just produced...1365180540
On loop 17, just produced...1540383426
On loop 18, just produced...304089172
On loop 14, I just consumed...2044897763
On loop 15, I just consumed...1967513926
On loop 16, I just consumed...1365180540
On loop 17, I just consumed...1540383426
On loop 18, I just consumed...304089172
On loop 19, just produced...1303455736
On loop 20, just produced...35005211
On loop 19, I just consumed...1303455736
On loop 20, I just consumed...35005211
On loop 21, just produced...521595368
On loop 22, just produced...294702567
On loop 21, I just consumed...521595368
On loop 22, I just consumed...294702567
On loop 23, just produced...1726956429
On loop 24, just produced...336465782
On loop 23, I just consumed...1726956429
On loop 24, I just consumed...336465782
On loop 25, just produced...861021530
On loop 26, just produced...278722862
On loop 25, I just consumed...861021530
On loop 26, I just consumed...278722862
On loop 27, just produced...233665123
On loop 28, just produced...2145174067
On loop 27, I just consumed...233665123
On loop 28, I just consumed...2145174067
On loop 29, just produced...468703135
On loop 29, I just consumed...468703135
And that is all for those numbers