pthread - Segmentation fault

gopi bhimavarapu gopimallikharjun@yahoo.com
Wed Apr 15 08:30:00 GMT 2009


Hi

Can anyone tell me what is wrong with this program.
It is always crashing at thread # 24068.


#include <stdio.h>
#include <time.h>

 #include <sys/time.h>

#include <pthread.h>
#include <errno.h>
#define _MULTI_THREADED
#define THREADS_COUNT 30000
#define WAIT_TIME_SECONDS       2


pthread_mutexattr_t mtxattr;
pthread_mutex_t mtx= PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond;
static int count2 = 1;

void pthread_Exit(int);
void *runn(void *arg)
{
	int i,ret,count;
		
         struct timeval    tp;
	 struct timespec   ts;

		pthread_mutex_lock(&mtx);
                count = count2;
		printf(" In thread %d Try Locking... \n",count2++);
		printf(" Locking OK thread %d\n", count);
    ret =  gettimeofday(&tp, NULL);
//    checkResults("gettimeofday()\n", ret);

    /* Convert from timeval to timespec */
    ts.tv_sec  = tp.tv_sec;
    ts.tv_nsec = tp.tv_usec * 1000;
    ts.tv_sec += WAIT_TIME_SECONDS;

	        pthread_cond_timedwait(&cond, &mtx, &ts);
		printf(" Release lock thread %d \n", count);
		pthread_mutex_unlock(&mtx);
		printf(" Release OK thread %d\n", count);
	

	//pthread_exit(NULL);
	pthread_Exit(count);
        return NULL;
}
void pthread_Exit(int count)
{
     printf("Exiting thread %d\n",count);
	pthread_exit(NULL);
}
  

int main()
{
	int i,j;

        pthread_attr_t attr;
	pthread_mutexattr_init(&mtxattr);
	pthread_mutexattr_settype(&mtxattr,PTHREAD_MUTEX_RECURSIVE_NP);
        pthread_mutex_init(&mtx,&mtxattr);

	for (i=0;i<THREADS_COUNT;++i)
        {
		pthread_create(&th[i],NULL,runn,NULL);
	}



	printf("I am here\n");
		pthread_mutex_lock(&mtx);
		pthread_mutex_unlock(&mtx);
	for (j=0;j<THREADS_COUNT;++j)
        {
	printf("I am here 2\n");
		pthread_join(th[j],NULL);
	}

	pthread_mutex_destroy(&mtx);
        pthread_mutexattr_destroy(&mtxattr);


	return 0;
}


Regards,
Gopi


      
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t4.c
Type: text/x-csrc
Size: 1720 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20090415/8dc925d2/attachment.bin>


More information about the Gcc-help mailing list