生产者消费者问题:C语言指针类型不兼容及输出异常求助
问题背景
尝试用mutex、条件变量和pthreads实现生产者消费者模型。已参考相关方案将线程函数改为void* funcname(void *args)格式,但仍存在以下问题:
- pthread_create参数3指针类型不兼容警告
- 字符串溢出警告
- 程序输出不完整
编译警告与程序输出
main.c: In function ‘main’:
main.c:33:42: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
33 | pthread_create(&thread[i], NULL, producer, &thread_id[i]);
| ^~~~~~~~
| |
| void * (*)(int )
In file included from main.c:2:
/usr/include/pthread.h:200:15: note: expected ‘void * ()(void )’ but argument is of type ‘void * ()(int *)’
200 | void (__start_routine) (void ),
|~~~~~~~~~~~~~~~~^
main.c:36:42: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
36 | pthread_create(&thread[i], NULL, consumer, &thread_id[i]);
| ^~~~~~~~
| |
| void * ()(int )
In file included from main.c:2:
/usr/include/pthread.h:200:15: note: expected ‘void * ()(void )’ but argument is of type ‘void * ()(int *)’
200 | void (__start_routine) (void *),
|~~~~~~~~~~~~~~~~^
main.c:30:5: warning: ‘__builtin_memcpy’ writing 7 bytes into a region of size 6 overflows the destination [-Wstringop-overflow=]
30 | strcpy(source, "abcdef");
| ^~~~~~~~~~~~~~~~~~~~~~~~
1 is produced abcdef in buffer
1 is consuming from buffer
0 is consuming from buffer
完整代码
#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <stdlib.h> #include <string.h> #define MAX 10 #define BUFLEN 6 #define NUMTHREAD 2 void* consumer(int *id); void* producer(int *id); char buffer[BUFLEN]; char source[BUFLEN]; int pCount = 0; int cCount = 0; int buflen; pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t nonEmpty = PTHREAD_COND_INITIALIZER; pthread_cond_t full = PTHREAD_COND_INITIALIZER; int thread_id[NUMTHREAD] = {0, 1}; int i = 0; int j = 0; int main() { int i; pthread_t thread[NUMTHREAD]; strcpy(source, "abcdef"); buflen = strlen(source); for (i = 0; i < NUMTHREAD; i++){ pthread_create(&thread[i], NULL, producer, &thread_id[i]); } for (i = 0; i < NUMTHREAD; i++){ pthread_create(&thread[i], NULL, consumer, &thread_id[i]); } for (i = 0; i < NUMTHREAD; i++){ pthread_join(thread[i], NULL); } printf("\n"); } void *producer(int *id) { int i; pthread_mutex_lock(&count_mutex); while (pCount == MAX) { printf("Producer %d is waiting for empty buffer\n", *id); pthread_cond_wait(&nonEmpty, &count_mutex); } for (i = 0; i < BUFLEN; i++) { buffer[i] = source[i]; } pCount++; printf("%d is produced %s in buffer\n", *id, source); pthread_cond_signal(&full); pthread_mutex_unlock(&count_mutex); } void *consumer(int *id) { int i; pthread_mutex_lock(&count_mutex); while (cCount == MAX) { printf("Consumer %d is waiting for full buffer\n", *id); pthread_cond_wait(&full, &count_mutex); } for (i = 0; i < BUFLEN; i++) { buffer[i] = '\0'; } cCount++; printf("%d is consuming %s from buffer\n", *id, buffer); pthread_cond_signal(&nonEmpty); pthread_mutex_unlock(&count_mutex); }
内容的提问来源于stack exchange,提问作者EmployedSlaveStudent

