我正在为学校做作业,作业的目标是使用 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