Thread: return value in a thread

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    return value in a thread

    Code:
    typedef struct
    {
        int fd;
        int state;
    }response_client;
    
    
    
    void *action()
    {  
        response_client resp;
        resp.fd = 2;
        resp.state = 1;
        pthread_exit(&resp);
    }
    
    
    
    
    
    
    
    int main()
    {
        pthread_t t1;
        void* fd;
        void* resp;
    
    
        if(pthread_create(&t1, NULL, &action, NULL) != 0)
    
        {
    
            fprintf(stderr, "error......\n");
            exit(0);
    
        }else{
    
            printf("ok!\n");
    
        }
    
        pthread_join(t1, &resp);
    
        printf("test %d\n\n",*(int*)resp->state);
    
    
        return 0;
    
    }
    Hey there...

    I have the problem to return the struct in the thread, i get every time after compiling:

    test.c:55: error: request for member ‘state’ in something not a structure or union

    I tried everything, but i dont get it to work. Can someone help me plz?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First off, resp1 is not initialized. It doesn't point to anything.
    Secondly, you can't dereference void pointers. If you know what type it is, then cast it to the proper type before dereferencing.
    Thirdly, why do you think an integer has any members (hint: it's not a struct).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Thirdly, why do you think an integer has any members (hint: it's not a struct).
    pthread_exit() takes a void pointer, not an int, so that part is okay. Or at least that is not what's wrong with it
    Last edited by MK27; 02-15-2010 at 03:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, here's an example of what you want to do:
    Code:
    #include  <stdio.h>
    #include  <string.h>
    #include  <stdlib.h>
    #include  <pthread.h>
    
    struct test {
    	char str[32];
    	int x;
    };
    
    void *tfunc() {
    	struct test *eg = malloc(sizeof(struct test));
    	strcpy(eg->str,"hello world");
    	eg->x = 666;
    	pthread_exit(eg);
    }
    
    
    int main (void) {
    	pthread_t id;
    	struct test *resp;
    
    	pthread_create(&id,NULL,tfunc,NULL);
    	pthread_join(id,(void**)&resp);
    
    	printf("%s %d\n",resp->str,resp->x);
    
    	free(resp);
    
    	return 0;
    }
    Take a close look at the pointer to a pointer used in the red line, and how the struct returned must be malloc'd global memory in tfunc().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Thank you for your help, it works! : D

    Pointer are a strange topic. In the german forum they have not responded until now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM