Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the thread function return 0?

I have this void function, with a pointer to my thread. when i go to compile, I get the warning: "control reaches end of non-void function". If I do

void mythread (void *arg)

and the function i will solve the warning that the compiler gives, but get a new warning, that says:

TA.c:50:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]

So then, I put return 0; at the end of "mythread" function, and it compiles. But I'm not sure if this is the right thing to do?

I am trying to learn POSIX system service programing. I am not sure what to do.

Advice? Should I try and type cast argument 3? How can I compile my program, and get zero warnings?

#include <limits.h>
#include <pthread.h>
#include <stdio.h>

void *mythread (void *arg)
{   
    printf("This my Thread Maybe...\n");    
}

int main()
{
    pthread_t mythread_id;
    pthread_attr_t mythread_attr;
    size_t stack_size;
    int detachstate;

    pthread_attr_init (&mythread_attr);
    pthread_attr_getdetachstate(&mythread_attr, &detachstate);
    if(detachstate == PTHREAD_CREATE_DETACHED)
    {
        printf("Current Deteached state is Detached\n");
    }
    else
    {
        printf("Current Detached state is Joinable\n");
    }

    pthread_attr_setdetachstate (&mythread_attr, PTHREAD_CREATE_DETACHED);
    pthread_attr_getdetachstate (&mythread_attr, &detachstate);

    if(detachstate == PTHREAD_CREATE_DETACHED)
    {
        printf("NEW DETACHED STATE is determined to be deteched\n");
    }
    else
    {
        printf("NEW DETACHED STATE is Determine to be Joinable\n");
    }

    pthread_attr_getstacksize (&mythread_attr, &stack_size);
    printf ("Default stack size is %d; minimum is %d\n", stack_size, PTHREAD_STACK_MIN);
    pthread_attr_setstacksize (&mythread_attr, PTHREAD_STACK_MIN*2);
    pthread_attr_getstacksize (&mythread_attr, &stack_size); 

    printf("NEW stack size is %d\n", stack_size);
    pthread_create (&mythread_id, &mythread_attr, mythread, 0);

    return 0;
}
like image 843
j0h Avatar asked Aug 12 '13 22:08

j0h


1 Answers

Adding return 0 to the end of the function is the right thing to do. The function signature is such that it should return a value. The pthread library code will expect a return value to be returned. Casting the thread function to the wrong signature would result in undefined behavior.

Your code can see the return result after a call to pthread_join(). You can use 0 to mean success. If that doesn't satisfy you, you can choose a different value.

like image 179
jxh Avatar answered Sep 28 '22 05:09

jxh