Problem with pthread_create() - passing argument for start_routine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erwann
    New Member
    • Mar 2007
    • 4

    Problem with pthread_create() - passing argument for start_routine

    On a Linux platform:
    Hi,

    I am just trying to pass an integer as an argument to my start_routine when creating a thread with pthread_create:

    In my .h file:
    short portfd;
    void *receive_talker (void* portfd);

    In main():
    thread_talker is declared as " pthread_t thread_talker; "
    pthread_create( &thread_talker, NULL,receive_ta lker,(void *)portfd); (line 125)

    In my .h file:
    short portfd;
    void *receive_talker (void* portfd);

    receive_talker is written like this after main():

    void *receive_talker (void *portfd) {
    ...
    }

    When compiling, I get the following message:

    "warning: cast to pointer from integer of different size "

    I also tried:

    pthread_create( &thread_talker, NULL,receive_ta lker,(void *)(&portfd));

    then I get:

    " Segmentation fault "

    ...and the execution stops

    from a printf("portfd %d\n", (int) portfd )) I can get:

    portfd = 268513978 when supposed to be smth like 1 or 2...

    I ve got the same 268513978 with :
    printf("portfd %d\n", (int *) portfd ))

    I guess something is wrong with either the way I pass my variable to the thread or with the way I use the passed variable in my start routine.

    I am not so familiar with C so if anybody can help...
    Thanks
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    try making this an int
    Code:
    int portfd;

    Comment

    Working...