这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
sem_t s1, s2;
pthread_t foo_tid, bar_tid;
void *foo(void*) {
while(1) {
sem_wait(&s1);
printf("HI ");
sem_post(&s2);
}
}
void *bar(void*) {
while(1) {
sem_wait(&s2);
printf("HO ");
sem_post(&s1);
}
}
int main() {
sem_init(&s1, 0, 0);
sem_getvalue(&s1, &foo_tid);
sem_init(&s2, 0, 0);
sem_getvalue(&s2, &bar_tid);
pthread_create(&foo_tid, NULL, foo, NULL);
pthread_create(&bar_tid, NULL, bar, NULL);
return 0;
}
我试图获取信号量s1
和s2
的输出。但我不断收到这些错误:
sema.c: In function 'main':
sema.c:29:3: warning: passing argument 2 of 'sem_getvalue' from incompatible pointer type [enabled by default]
In file included from sema.c:6:0:
/usr/include/semaphore.h:72:12: note: expected 'int * __restrict__' but argument is of type 'pthread_t *'
sema.c:31:3: warning: passing argument 2 of 'sem_getvalue' from incompatible pointer type [enabled by default]
In file included from sema.c:6:0:
/usr/include/semaphore.h:72:12: note: expected 'int * __restrict__' but argument is of type 'pthread_t *'`
我还没有摆脱这个错误。如果有人能帮我解决这个问题,那将非常感激!
答案 0 :(得分:0)
从sem_getvalue开始,值应为int:
声明int s1_val, s2_val;
sem_init(&s1, 0, 0);
sem_getvalue(&s1, &s1_val);
sem_init(&s2, 0, 0);
sem_getvalue(&s2, &s2_val);
答案 1 :(得分:0)
第二个参数应该是指向int
的指针,当你传递pthread_t
的地址时。 pthread_t
不是您系统中的int
。