This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

pthread - Segmentation fault


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


      
#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;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]