Code(using pthreads) working in gcc2.96,NOT working in gcc3.2

Ulrich Prakash uprakash@npd.hcltech.com
Mon Mar 24 14:53:00 GMT 2003


Hi all,

The following code works fine in gcc2.96 but CRASHES in gcc3.2..
The code is a simple program demonstrating the use of mutex....

Please help me find out the reason for the CRASH,as also i could not use 
GDB with gcc3.2(but could use well with gcc2.96)...
**************OUTPUT***********
[uprakash@voicedsp trial]$ ./a.out
Thread started
Main Program
New Thread id is 1026l
Parent Thread id is 1024l
End of Program
Segmentation fault
[uprakash@voicedsp trial]$
*********END OF OUTPUT****************

*********CODE**********************
#include <pthread.h>

static pthread_mutex_t  mutex_part = PTHREAD_MUTEX_INITIALIZER;

void start_proc();

int main()
{
         pthread_attr_t    attr;
         pthread_attr_t    *temp_attr = NULL;
         pthread_t *thread_ptr;

         if (0 != pthread_attr_init (&attr)) {
                 perror ("pthread_attr_init");
                 goto thr_init;
         }
         if (0 != pthread_attr_setdetachstate (&attr, 
PTHREAD_CREATE_DETACHED)) {
                 perror ("pthread_attr_setdetachstate");
                 goto thr_init;
         }
         temp_attr = &attr;


thr_init:
          if (0 != pthread_create (thread_ptr, temp_attr, (void 
*)start_proc, NULL)) {
                 perror ("pthread_create");
         }

     printf ("Main Program\n");

     sleep (5);
     pthread_mutex_lock (&mutex_part);
     printf ("Parent Thread id is %ul\n",pthread_self() );
     pthread_mutex_unlock (&mutex_part);

     printf ("End of Program\n");

     return 0;

}


void start_proc()
{

     printf ("Thread started\n");
     pthread_mutex_lock (&mutex_part);
     sleep (15);
     printf ("New Thread id is %ul\n",pthread_self() );
     pthread_mutex_unlock (&mutex_part);

     pthread_exit (NULL);


}



More information about the Gcc-help mailing list