0

我正在为学校做作业,作业的目标是使用 Pthread Barriers,使树莓派上的 LED 慢慢地从未点亮变为点亮,反之亦然。我是 C 的初学者,我想我理解 pthread sna dn pthread 障碍的概念。

以下是我编写的代码,它应该使 raspPI 中的 LED 从未点亮变为点亮:

// Setup wiringPi
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// define led output PINS
#define LED1 7
#define LED2 0
#define LED3 2
#define LED4 3

pthread_barrier_t* mythread;

void ledInit(void){
    // Initialize pin for PMW output
    softPwmCreate(LED1, 0, 100);
    softPwmCreate(LED2, 0, 100);
    softPwmCreate(LED3, 0, 100);
    softPwmCreate(LED4, 0, 100);
}

// a thread that slowly fades LEDs from unlit to lit. 
void* unlitToLit(void){
    ledInit();
    softPwmWrite(LED1, 50);
    softPwmWrite(LED2, 50);
    softPwmWrite(LED3, 50);
    softPwmWrite(LED4, 50);
}

void main(void){
    wiringPiSetup();
    //set LED1 pin to output
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(LED4, OUTPUT);

    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);

    pthread_barrier_init(&mythread, NULL, unlitToLit);

    if (pthread_create(&mythread, NULL, unlitToLit, )) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    printf("threads finished");
    return 0;

      // used for synchronization
      // pthread_barrier_wait(&mythread);
}

在我看来,代码几乎是正确的,但它不起作用,请你帮我找出并解决问题。

错误/警告列表:

3_1.c:15:5: warning: implicit declaration of function ‘softPwmCreate’ [-Wimplicit-function-declaration]
     softPwmCreate(LED1, 0, 100);
     ^
3_1.c: At top level:
3_1.c:21:1: warning: return type defaults to ‘int’
 unlitToLit(void){
 ^
3_1.c: In function ‘unlitToLit’:
3_1.c:23:5: warning: implicit declaration of function ‘softPwmWrite’ [-Wimplicit-function-declaration]
     softPwmWrite(LED1, 50);
     ^
3_1.c: At top level:
3_1.c:28:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(void){
      ^
3_1.c: In function ‘main’:
3_1.c:40:43: warning: passing argument 3 of ‘pthread_barrier_init’ makes integer from pointer without a cast
     pthread_barrier_init(&mythread, NULL, unlitToLit);
                                           ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:1079:12: note: expected ‘unsigned int’ but argument is of type ‘int (*)(void)’
 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
            ^
3_1.c:42:53: error: expected expression before ‘)’ token
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                                                     ^
3_1.c:42:24: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                        ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:244:12: note: expected ‘pthread_t * restrict’ but argument is of type ‘union pthread_barrier_t *’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
3_1.c:42:41: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                                         ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:244:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int (*)(void)’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
3_1.c:44:9: warning: ‘return’ with a value, in function returning void
         return 1;
         ^
3_1.c:48:5: warning: ‘return’ with a value, in function returning void
     return 0;
     ^
3_1.c: In function ‘unlitToLit’:
3_1.c:27:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
makefile:12: recipe for target '3_1' failed
4

2 回答 2

1
   pthread_barrier_init(&mythread, NULL, unlitToLit);

mythread变量被定义为指针,因此您在此处传递指针的内存地址。您应该mythread如下定义变量:

pthread_barrier_t mythread;

此外,使用障碍(以及恕我直言的分配)的目的是同步线程,以便它们从某个稳定的共享状态继续执行。在您的情况下,所有 LED 都会点亮或熄灭。您需要添加pthread_barrier_wait以同步它们。

还有一个问题:

pthread_barrier_init(&mythread, NULL, unlitToLit);

第三个参数是count用来指定有多少线程应该调用pthread_barrier_wait屏障解锁。在您的通话中,您传递了一个错误的函数地址。

于 2018-04-06T13:45:24.000 回答
0

通过阅读您正在使用的库(软件 PWM 库参考)的文档,我可以看到您需要包括:

#include <softPwm.h>

我还可以为函数 softPwmWrite() 阅读此内容:

void softPwmWrite (int pin, int value) ;

这会更新给定引脚上的 PWM 值。检查该值是否在范围内,并且先前未通过softPwmCreate初始化的引脚将被静默忽略。我看不到您已将引脚初始化为 PWM 引脚。

如果您确实希望 LED 褪色,您还需要更改脉冲宽度... softPwmWrite(LED1, 50); 将脉冲宽度设置为 50 %,这将使 LED “半”亮。您需要反复将 PWM 从 0 更改为 100,反之亦然...

于 2018-04-06T13:57:59.907 回答