gpt4 book ai didi

c - 信号量值的输出

转载 作者:行者123 更新时间:2023-11-30 19:39:03 24 4
gpt4 key购买 nike

这是我的代码:

#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;
}

我正在尝试获取信号量 s1s2 的输出。但我不断收到这些错误:

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 *'`

我只是无法摆脱这个错误。如果有人能帮助我解决这个问题,我将不胜感激!

最佳答案

来自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);

关于c - 信号量值的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com