Pthread-barrier和raspberry pi

时间:2018-04-06 13:40:06

标签: c multithreading raspberry-pi pthreads pthread-barriers

我正在为学校做作业,而作业的目标是使用Pthread障碍,使树莓派上的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

2 个答案:

答案 0 :(得分:1)

   pthread_barrier_init(&mythread, NULL, unlitToLit);

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

pthread_barrier_t mythread;

使用障碍(以及IMHO赋值)的目的也是为了同步线程,以便它们从某个稳定的共享状态继续执行。在您的情况下,所有LEDS点亮或熄灭。您需要添加pthread_barrier_wait才能同步它们。

还有另一个问题:

pthread_barrier_init(&mythread, NULL, unlitToLit);

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

答案 1 :(得分:0)

通过阅读您正在使用的库(Software PWM Library reference)的文档,我可以看到您需要包含:

#include <softPwm.h>

我还可以阅读函数softPwmWrite():

void softPwmWrite (int pin, int value) ;

这会更新给定引脚上的PWM值。检查该值是否在范围内,以及之前未通过 softPwmCreate 初始化的引脚将被静默忽略。我看不出你已经将引脚初始化为PWM引脚。

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