这是我的密码:

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”。

最佳答案

pthread_create的最后一个参数是问题。您将指针传递给i,但是i的值在主循环执行时会发生变化。当线程执行打印并遵从指针时,不能保证i仍然是0(或123NUM_CONSUMERS)。
尝试这个简单的更改;它将为您提供预期的结果:

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_PRODUCERSint是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的引用,将其转换为:
printf("producer thread: #%d in critical section\n", (int)arg);

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

注意:在64位系统上,将在指针和int之间转换警告。

关于c - C中的Pthread_Create导致奇怪的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22925093/

10-17 01:16