0
char buff[MAX_SIZE];

int main() {

    pthread_t thread[3];

    char message1 = 17;  //17 = 0x11
    char message2 = 18;
    char message3 = 19;
    char message4 = 20;

    int iret[3];
    int k;

    char message[] = {17, 18,19,20};

    /*THIS IS WORKING  

    iret[0] = pthread_create( &thread[0], NULL, write_to_buffer, (void*) &message1);
    iret[1] = pthread_create( &thread[1], NULL, write_to_buffer, (void*) &message2);
    iret[2] = pthread_create( &thread[2], NULL, write_to_buffer, (void*) &message3);
    iret[3] = pthread_create( &thread[3], NULL, write_to_buffer, (void*) &message4);
    */


    /* BUT THIS IS NOT 

    iret[0] = pthread_create( &thread[0], NULL, write_to_buffer, (void*) message);
    iret[1] = pthread_create( &thread[1], NULL, write_to_buffer, (void*) (message+1));
    iret[2] = pthread_create( &thread[2], NULL, write_to_buffer, (void*) (message+2));
    iret[3] = pthread_create( &thread[3], NULL, write_to_buffer, (void*) (message+3));

    */

    for(k=0;k<=3;k++)   {
        pthread_join( thread[k], NULL);
    }


    //rest of main

}


void *write_to_buffer( void *ptr ) {

    while(1)    {
        pthread_mutex_lock(&my_mutex);


        char *message;
        message = (char *) ptr;

        //when passing by array I'm unable to get the value in the message variable

        printf("\nMeeee = %#x  ", *(char*)ptr);



        //REST OF THE FUNCTION //logic to write to buffer

        pthread_mutex_unlock(&my_mutex);

        //SOME LOGIC I OMMITED HERE
        //to return if(indexx >= MAX_SIZE) return(NULL);        
    }   
}

the problem i'm facing is that when i'm passing the array element i'm unable to capture the value in the thread function. but when i'm passing address of message1, message2, message3, and message4 i'm able to get the value passed in thread function

5
  • 1
    To start with, there's no need for the (void*) typecast, all pointers can be passed as void* without casting. To continue, what's the actual output of your code? Commented Oct 28, 2013 at 9:25
  • 1
    What do you get when passing message (the 2nd case)? Commented Oct 28, 2013 at 9:26
  • 2
    BTW: There are 4 threads, but the array of threads is only 3 long. Commented Oct 28, 2013 at 9:26
  • ok corrected the join but still the address of array is not passed properly Commented Oct 28, 2013 at 9:29
  • @oomkiller So did you correct the array of threads too ? It must be pthread_t thread[4]; instead of pthread_t thread[3]; Possibly you must correct int iret[3]; too, any anything else that is wrong that isn't in the posted code. Commented Oct 28, 2013 at 11:58

1 Answer 1

1

You have a couple of cases of undefined behavior in your code. You declare the thread and iret arrays of size three, but create four threads thereby overwriting beyond the bounds of these arrays. This might affect the result of your program and its output, as it might cause the data you pass to be overwritten.

Sign up to request clarification or add additional context in comments.

2 Comments

but array starts from 0 so total of 4
@oomkiller Yes, when you index them, not when you declare them. Then you have to tell the compiler the actual size, which is 4 in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.