Thread: structure not showing any value

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    structure not showing any value

    hi,
    following is the code in which i have created two threads and two these two threads i have passed one structure.
    for the second thread, in the threadfunction, it shows the value fo charatcer and count as empty !!
    any idea what i am doing wrong?
    thanx
    deepak


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

    struct str1
    {
    char character;
    int count;
    };
    void* threadfunction(void* param)
    {
    struct str1* p = (struct str1*)param;
    int i;
    printf("the character to be printed is %c and the number of time is %d\n\n",p->character, p->count);
    for(i = 0; i< p->count; i++)
    printf("%c",p->character);
    return NULL;
    }

    int main()
    {
    pthread_t thread1_id;
    pthread_t thread2_id;
    struct str1 structthread1;
    struct str1 structthread2;

    structthread1.character='D';
    structthread1.count=5;



    pthread_create(&thread1_id, NULL, &threadfunction, &structthread1);


    structthread2.character='S';
    structthread2.count=7;

    // printf("the character to be printed is %c and the number of time is %d\n\n",structthread1.character,structthread1.coun t);
    // printf("the character to be printed is %c and the number of time is %d\n\n",structthread2.character, structthread2.count);

    //pthread_create(&thread1_id, NULL, &threadfunction, &structthread1);
    pthread_create(&thread2_id, NULL, &threadfunction, &structthread2);

    return 0;

    }

  2. #2
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    use code tags.

    use code tags to make it clear.Some people might then want to help you.
    click here to know what code tags are.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    16
    You should use the function pthread_join(&pthread_t) to make the main function to wait for the thread to finish. Since you are passing structure that is local to main, The main function should finish its execution last.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM