[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

pthread_exit?



Hello,
I downloaded this program from the net, compiled it and ran. It works as

it is supposed to. But the problem is, redirection of its output is not
happening.  The reason I think is 'pthread_exit'. In the program both
the
main thread and the thread created is stopped using 'pthread_exit'.
And, it looks like 'pthread_exit' is not doing a 'fflush'.  Why is this?

If I call 'fflush'  before 'pthread_exit'  everything works fine.

Have a look at the code....

-- pthread_create.c --
#include <stdio.h>       /* standard I/O routines                 */
#include <pthread.h>     /* pthread functions and data structures */

/* function to be executed by the new thread */
void*
do_loop(void* data)
{
    int i;   /* counter, to print numbers */
    int j;   /* counter, for delay        */
    int me = *((int*)data);     /* thread identifying number */

    for (i=0; i<10; i++) {
        for (j=0; j<500000; j++) /* delay loop */
                  ;
        printf("'%d' - Got '%d'\n", me, i);
    }

    /* exit the thread */
    pthread_exit(NULL);
 }

/* like any C program, program's execution begins in main */
int
main(int argc, char* argv[])
{
    int        thr_id;         /* thread ID for the newly created thread
*/
    pthread_t  p_thread;       /* thread's structure
*/
    int        a         = 1;  /* thread 1 identifying number
*/
    int        b         = 2;  /* thread 2 identifying number
*/

    /* create a new thread that will execute 'do_loop()' */
    thr_id = pthread_create(&p_thread, NULL, do_loop, (void*)&a);
    /* run 'do_loop()' in the main thread as well */
    do_loop((void*)&b);

    /* NOT REACHED */
    return 0;
}

-- pthread_create.c --


Thanks in advance.

Regards,
Sunil