For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2007 > well i dont get the expected output









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author well i dont get the expected output
Ravi

2007-07-20, 7:10 pm

#include <stdio.h>
#include <pthread.h>
struct char_n {
char c;
int i;
};
struct char_n cn1 = {'X',10};

void *print_n_char(void *v)
{
int i;
struct char_n *cn = (struct char_n *)v;
for(i=0;i < cn->i;i++)
fprintf(stderr,"%c ",cn->c);
return NULL;
}

int main(void)
{
pthread_t tid1;
pthread_attr_t pattr;
pthread_attr_init(&pattr);
pthread_attr_setdetachstate(&pattr,PTHREAD_CREATE_DETACHED);
pthread_create(&tid1,&pattr,&print_n_char,&cn1);
pthread_attr_destroy(&pattr);
return 0;
}

However output to the screen is no here since main thread returns
before thread tid1. --> but after making cn1 gloabl I ensure it
shouldn't be deallocated when the main thread exits. But still thread
tid1 is not printing X's on the screen.

David Schwartz

2007-07-21, 4:16 am

On Jul 20, 8:28 am, Ravi <ra.ravi....@gmail.com> wrote:
> #include <stdio.h>
> #include <pthread.h>
> struct char_n {
> char c;
> int i;};
>
> struct char_n cn1 = {'X',10};
>
> void *print_n_char(void *v)
> {
> int i;
> struct char_n *cn = (struct char_n *)v;
> for(i=0;i < cn->i;i++)
> fprintf(stderr,"%c ",cn->c);
> return NULL;
>
> }
>
> int main(void)
> {
> pthread_t tid1;
> pthread_attr_t pattr;
> pthread_attr_init(&pattr);
> pthread_attr_setdetachstate(&pattr,PTHREAD_CREATE_DETACHED);
> pthread_create(&tid1,&pattr,&print_n_char,&cn1);
> pthread_attr_destroy(&pattr);
> return 0;
>
> }
>
> However output to the screen is no here since main thread returns
> before thread tid1. --> but after making cn1 gloabl I ensure it
> shouldn't be deallocated when the main thread exits. But still thread
> tid1 is not printing X's on the screen.


You return 0 from 'main'. This is equivalent to calling exit(0). The
thread you created may never get a chance to run.

DS

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com