我有10个使用者线程和1个生产者线程。生产者线程产生一个随机整数并将其插入缓冲区。使用者线程从该缓冲区中取出一个项目并将其打印出来。
一切都很好(我在里面,没有无限循环或阻塞的东西)。但是我认为,只有一个工作的使用者线程是第10个(最后一个)使用者线程。其他9个使用者线程不起作用。我意识到,当我使用使用者线程方法打印使用者ID时。为什么其他9个使用者线程不起作用,这种问题又能做什么呢?
下面是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
typedef int buffer_item;
#define BUFFER_SIZE 5
#define RAND_DIVISOR 100000000
#define TRUE 1
pthread_mutex_t mutex;
sem_t full, empty;
buffer_item buffer[BUFFER_SIZE];
int counter;
pthread_t tid;
pthread_attr_t attr;
void *producer(void *param);
void *consumer(void *param);
int insert_item(buffer_item item) {
if(counter < BUFFER_SIZE) {
buffer[counter] = item;
counter++;
return 0;
}
else {
return -1;
}
}
int remove_item(buffer_item *item) {
if(counter > 0) {
*item = buffer[(counter-1)];
counter--;
return 0;
}
else {
return -1;
}
}
void initializeData() {
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
pthread_attr_init(&attr);
counter = 0;
}
void *producer(void *param) {
buffer_item item;
while(TRUE) {
int rNum = rand() / RAND_DIVISOR;
sleep(1);
item = rand()%100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
if(insert_item(item)) {
fprintf(stderr, " Producer report error condition\n");
}
else {
printf("producer produced %d\n", item);
}
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void *consumer(void *param) {
buffer_item item;
int* consumerID=(int*)param;
while(TRUE) {
int rNum = rand() / RAND_DIVISOR;
sleep(1);
sem_wait(&full);
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
fprintf(stderr, "Consumer report error condition\n");
}
else {
printf("consumer %d consumed %d\n" ,*consumerID, item);
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main(int argc, char *argv[]) {
/* Loop counter */
int i;
int numProd = 1; /* Number of producer threads */
int numCons = 10; /* Number of consumer threads */
/* Initialize the app */
initializeData();
/* Create the producer threads */
for(i = 0; i < numProd; i++) {
/* Create the thread */
pthread_create(&tid,&attr,producer,NULL);
}
/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid,&attr,consumer,(void*)&i);
}
/* Sleep for the specified amount of time in milliseconds */
sleep(10);
/* Exit the program */
printf("Exit the program\n");
exit(0);
}
我的输出是:
producer produced 27
consumer 10 consumed 27
producer produced 63
consumer 10 consumed 63
producer produced 26
consumer 10 consumed 26
producer produced 11
consumer 10 consumed 11
producer produced 29
consumer 10 consumed 29
producer produced 62
consumer 10 consumed 62
producer produced 35
consumer 10 consumed 35
producer produced 22
consumer 10 consumed 22
producer produced 67
consumer 10 consumed 67
Exit the program
答案 0 :(得分:3)
肖恩击败了我,但是的,他是正确的。请参见以下实现:
/* Consumer Thread */
void *consumer(void *param) {
buffer_item item;
int* consumerID=(int*)param;
printf("consumer %d created\n" ,*consumerID);
while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(1);
/* aquire the full lock */
sem_wait(&full)%100;
/* aquire the mutex lock */
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
//fprintf(stderr, "Consumer report error condition: consumer %d item: %d\n", *consumerID, item);
}
else {
printf("consumer %d consumed %d\n" ,*consumerID, item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal empty */
sem_post(&empty);
}
}
long taskids[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
taskids[i] = i;
/* Create the thread */
pthread_create(&tid,&attr,consumer, taskids + i);
}
这将导致:
consumer 0 created
consumer 1 created
consumer 2 created
consumer 5 created
consumer 3 created
consumer 6 created
consumer 9 created
consumer 7 created
consumer 8 created
consumer 4 created
producer produced 65
consumer 9 consumed 65
producer produced 57
consumer 6 consumed 57
producer produced 33
consumer 5 consumed 33
producer produced 57
consumer 1 consumed 57
producer produced 3
consumer 3 consumed 3
producer produced 81
consumer 9 consumed 81
producer produced 1
consumer 5 consumed 1
答案 1 :(得分:2)
在:
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid,&attr,consumer,(void*)&i);
您正在向每个线程传递指向i
的指针,并保持指向该指针的指针,该指针将遵循i
的值
您需要在线程函数的开头复制到一个新变量,但重要的是要知道在调用pthread_create时线程函数不会立即启动。他们很可能直到循环结束且i = 10时才会开始。因此,可能发生的事情是您实际上有10个使用者,但他们都有相同的数量。
如果要使用i
作为ID,则应在创建新线程之前等待目标线程的信号量(从* param分配后将被发布)。
如果您希望对此进行非常简单的测试,则可以在调用每个pthread_create之后添加sleep(1)调用。那应该给每个线程时间启动并正确分配一个ID。