threading problem

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

void *print_message_function( void *ptr );

main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

    /* Create independent threads each of which will execute function */

     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     exit(0);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}

im new to the threading concept.the code im trying to execute above, it shows no errors on compiling . but the program goes into an infinite loop when executed. any idea as to where im wrong
??? running the code on HP-UX 11.11

/usr/lib/dld.sl: Unresolved symbol: U_get_unwind_table (code) from /opt/langtools/lib/libpthread_tr.1
/usr/lib/dld.sl: Unresolved symbol: U_get_unwind_table (code) from /opt/langtools/lib/libpthread_tr.1
/usr/lib/dld.sl: Unresolved symbol: U_get_unwind_table (code) from /opt/langtools/lib/libpthread_tr.1
Pid 2998 received a SIGSEGV for stack growth failure.
Possible causes: insufficient memory or swap space,
or stack size exceeded maxssiz.
Segmentation fault (core dumped)

code tags for code please. [ code ] stuff [ /code ] without the extra spaces in the tags. It preserves spaces and makes code readable. I'm not sure the code is compilable in its current state with things like the return value for main missing.

I think you need to put pthread_exit() at the bottom of the thread. This is also where the thread's return value is passed.

Also, you did not specify the threads as joinable, hence join should not be used on them. To make them joinable:

int main()
{
        pthread_attr_t attr;
        pthread_t thread1, thread2;
        char *message1 = "Thread 1";
        char *message2 = "Thread 2";
        int iret1, iret2;

        /* Set up attributes for the threads */
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

        /* Create independent threads each of which will execute function */

        iret1 = pthread_create( &thread1, &attr, print_message_function, (void*) message1);
        iret2 = pthread_create( &thread2, &attr, print_message_function, (void*) message2);

        /* Wait till threads are complete before main continues. Unless we */
        /* wait we run the risk of executing an exit which will terminate */
        /* the process and all threads before the threads have completed. */

        pthread_join( thread1, NULL);
        pthread_join( thread2, NULL);

        printf("Thread 1 returns: %d\n",iret1);
        printf("Thread 2 returns: %d\n",iret2);
        exit(0);
}

The code seems to be good, but unfortunately I don't know to much about HP-UX, but you could try running the threads one by one in order to narrow down the problem.

This code works fine in a i386 architecture.

bye.

thanks a lot corona , totally new to threads and im still learning . specified the thread as joinable and the return value for main , it works now :slight_smile: