pigpio
pigpio copied to clipboard
gpioStartThread
I think the documentation for gpioStartThread is missing a return argument in the example for void *myfunc(void *arg) { }
warning: control reaches end of non-void function [-Wreturn-type]
http://abyz.me.uk/rpi/pigpio/cif.html#gpioStartThread
I don't see a problem with the example.
The warning is for main() which as declared should return an int. Perhaps we should add a return 0; after the gpioTerminate();.
myfunc() should return a pointer to.... Something apparently. It's a void* return type. What does it return?
It should be void myfunc not void *myfunc. It looks like the prototype is wrong. I'm not going to change it. Just accept it is wrong and ignore it.
Not my day. The prototype and code are correct. Nothing is returned as the thread does not terminate. It is a while forever loop.
The following is a better example.
#include <stdio.h>
#include <unistd.h>
#include <pigpio.h>
void *myfunc(void *arg)
{
while (1)
{
printf("%s\n", (char *)arg);
sleep(1);
}
}
int main(int argc, char *argv[])
{
pthread_t *p1, *p2, *p3;
if (gpioInitialise() < 0) return 1;
p1 = gpioStartThread(myfunc, "thread 1"); sleep(3);
p2 = gpioStartThread(myfunc, "thread 2"); sleep(3);
p3 = gpioStartThread(myfunc, "thread 3"); sleep(3);
gpioStopThread(p3); sleep(3);
gpioStopThread(p2); sleep(3);
gpioStopThread(p1); sleep(3);
gpioTerminate();
return 0;
}
gpioThreadFunc_t returns a pointer. gpioStartThread() calls pthread_create() so the return from gpioThreadFuncs are passed to pthread_exit(). A return of NULL here would remove the warning and improve documentation.