C中的Pthread_Create导致奇怪的输出

时间:2014-04-07 23:45:47

标签: c multithreading operating-system producer-consumer

这是我的代码:

int BufferSize = 3;
int buffer[3] = {0,0,0};

int producer_cursor = 0;
int consumer_cursor = 0;

sem_t empty, mutex, full;

void* Producer(void *arg)
{

    sem_wait(&empty);
    sem_wait(&mutex);

    printf("producer thread: #%d in critical setion\n", *((int*) arg));
    int j;
    for (j=0;j<BufferSize;j++) {
        printf("%d", buffer[j]);
    }
    printf("\n");
    buffer[producer_cursor] = 1;
    producer_cursor = (++producer_cursor) % 3;

    for (j=0;j<BufferSize;j++) {
        printf("%d", buffer[j]);
    }
    printf("\n");
    sem_post(&full);
    sem_post(&mutex);
}

void * Consumer(void* arg)
{
   sem_wait(&full);
   sem_wait(&mutex);    

   printf("consumer thread: #%d in critical setion\n", *((int*) arg));
   int j;
   for (j=0;j<BufferSize;j++) {
      printf("%d", buffer[j]);
   }
   printf("\n");

   buffer[consumer_cursor] = 0;
   consumer_cursor = (++consumer_cursor) % 3;
   printf("after change\n");
   for (j=0;j<BufferSize;j++) {
       printf("%d", buffer[j]);
   }
   printf("\n");

   sem_post(&empty);
   sem_post(&mutex);
}

void main(int argc, char * argv[])
{
   int n; 
   pthread_t *threads;

   sem_init(&mutex, 0, 1);
   sem_init(&empty, 0, 3);
   sem_init(&full, 0, 0);   

   pthread_t consumers[NUM_COMSUMERS];
   pthread_t producers[NUM_PRODUCERS];

   int i;
   for (i = 0; i < NUM_COMSUMERS; i++) {
       pthread_create(consumers+i, NULL, Consumer, &i);
   }
   for (i = 0; i < NUM_PRODUCERS; i++) {
       pthread_create(producers+i, NULL, Producer, &i);
   }
   for (i = 0; i < NUM_COMSUMERS; i++) {
       pthread_join(consumers[i], NULL);
   }
   for (i = 0; i < NUM_PRODUCERS; i++) {
       pthread_join(producers[i], NULL);
   }
}

我得到的输出是:

producer thread: #1 in critical setion
000
100
consumer thread: #4 in critical setion
100
after change
000
producer thread: #0 in critical setion
000
010
consumer thread: #1 in critical setion
010
after change
000
producer thread: #1 in critical setion
000
001
producer thread: #2 in critical setion
001
101
consumer thread: #2 in critical setion
101
after change
100
consumer thread: #2 in critical setion
100
after change
000
producer thread: #2 in critical setion
000
010
consumer thread: #4 in critical setion
010
after change
000

结果是预期的,除了计数应该在0到4之间,而在这种情况下它没有“#3”。每次运行程序时,“#”都会改变。有时它甚至只显示“#0”和“#1”。

1 个答案:

答案 0 :(得分:2)

pthread_create的最后一个参数是问题。您将指针传递给i,但i的值会随着主循环的执行而更改。当您的线程执行打印并且您遵循指针时,无法保证i仍然是0(或123)。< / p>

尝试这个简单的改变;它会给你预期的结果:

int i;
int id[4] = { 1, 2, 3, 4 };
for (i = 0; i < NUM_COMSUMERS; i++) {
    pthread_create(consumers+i, NULL, Consumer, id + i);
}
for (i = 0; i < NUM_PRODUCERS; i++) {
    pthread_create(producers+i, NULL, Producer, id + i);
}

以上假定NUM_CONSUMERSNUM_PRODUCERS为4,需要重新设计以使其更通用。

或者,您没有必要传入指针,您可以执行以下操作:

for (i = 0; i < NUM_COMSUMERS; i++) {
    pthread_create(consumers+i, NULL, Consumer, (void *)i);
}
for (i = 0; i < NUM_PRODUCERS; i++) {
    pthread_create(producers+i, NULL, Producer, (void *)i);
}

在你的主题中,不要取消引用arg,只需将其转换为int

printf("producer thread: #%d in critical section\n", (int)arg);

printf("consumer thread: #%d in critical section\n", (int)arg);

注意:在64位系统上,指针和int之间会出现警告。