PDA

View Full Version : structure not showing any value



sainideepak
01-27-2003, 05:45 AM
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.count);
// 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;

}

newbie_grg
01-27-2003, 07:07 AM
use code tags to make it clear.Some people might then want to help you. :)
click here to know what code tags are. (http://cboard.cprogramming.com/misc.php?s=&action=bbcode)

lsme
02-28-2003, 11:25 PM
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.