All pastes #1029814 Raw Edit

Miscellany

public text v1 · immutable
#1029814 ·published 2008-05-26 11:32 UTC
rendered paste body
#include <stdio.h>
#include <pthread.h>

//int k;

/* This is our thread function.  It is like main(), but for a thread */
void *threadFunc(void *arg)
{
//	char *str;
//	int i = 0;

//	str=(char*)arg;
	int *j = (int *) arg;
//	int **j = (int **) arg;
	while(1)
	{
		if(*j == 1)
		{
				printf("First thread\n");
		}
		else if(*j == 2)
		{
			printf("Second Thread\n");
		}
		else if (*j == 3)
		{
			printf("Third Thread\n");
		}
		sleep(1);
	}
	/*	int k;
	k= 5*(*j);
	while(i < 10 )
	{
		sleep(1);
		printf("threadFunc says: %d %d\n",k,*j);
		++i;
	}
*/
	return NULL;
}

int main(void)
{
	pthread_t pth;	// this is our thread identifier
	int i,*ptr;
	ptr=malloc(sizeof(int));
	*ptr=6;

//	int j = *ptr;	/* Create worker thread */
//	printf("%d\n",j);

	for(i = 1;i<=3;i++)
	{
		ptr=(int *)malloc(sizeof(int));
		*ptr=i;
		pthread_create(&pth,NULL,threadFunc,ptr);
//		*ptr = *ptr + 1;
//		j=*ptr;
		
	}


/*	

	while(i < 10 )
	{
		usleep(0);
		printf("main() is running...\n");
		++i;
	}
	pthread_join(pth, NULL);

	printf("aaamain() is running...\n");
*/
	while(1);
	return 0;
}