Tuesday, August 13, 2013

Return Value From Thread

As we have seen in the examples the return type of the functions that runs as the threads is the void*,it means we can return a value from the thread,but the point is how to capture that returned value in the main function...
The answer is the join again,as we have seen in the join the function prototype is
int pthread_join(pthread_t,void **)
Here the 2nd argument is the pointer to the value returned by the thread...
In the main funtion we can capture and use the value returned by the thread using the pthread_join.

Let us capture the return value...
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void* myfunc1(void *ptr)
{
    char *msg;
    int i=0;
    msg=(char*)ptr;
    while(1)
    {
        sleep(1);
        if(i>5)
            break;
        else
        {
            printf("i in thread1=%d\n",i);i++;
        }
    }
    printf("Thread1 completed\n");
    return "hello";
}
void *myfunc2(void *ptr)
{
    char *msg;
    int i=0;
    msg=(char*)ptr;
    while(1)
    {
        sleep(1);
        if(i>10)
            break;
        else
        {
            printf("i in thread2=%d\n",i);i++;
        }
    }
    printf("Thread2 completed\n");
    return "Hi";
}
int main()
{
    pthread_t thread1,thread2;
    int val1,val2;
    int status;
    char *msg1="Thread1";
    char *msg2="Thread2";
    void **ret_val1=malloc(sizeof(void*));
    void **ret_val2=malloc(sizeof(void*));
    printf("In the main funciton\n");
    pthread_create(&thread1,NULL,myfunc1,(void*)msg1);
    pthread_create(&thread2,NULL,myfunc2,(void*)msg2);
    pthread_join(thread1,ret_val1);
    pthread_join(thread2,ret_val2);
    printf("return value from thread1  is:%s\n",*ret_val1);
    printf("return value from thread2 is:%s\n",*ret_val2);
    printf("Now main ends");
    return 0;
}