pthread 본문

Programming/C

pthread

halatha 2011. 5. 6. 06:40
//	pthread1.c
#include 
#include 
#include 

pthread_t	threads[5];
int	done[5];

void* thread_main(void*);

//	http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Thread/Beginning/PthreadApiReference
//	http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Thread/Beginning/Example_pthread
//	gcc -o pthread1 pthread1.c -lpthread
int main(void)
{
	int	i, rc, status;

	printf("pid = %d\n", getpid());

	for ( i = 0; i < 5; ++i )
	{
		done[i]	=	0;
		pthread_create(&threads[i], NULL, &thread_main, (void *)i);
		printf("%d, %d\n", i, threads[i]);
	}

	for ( i = 4; i >= 0; --i )
	{
		done[i]	=	1;
		rc	=	pthread_join(threads[i], (void**)&status);
		if ( rc == 0 )
		{
			printf("Completed join with %d status = %d\n", i, status);
		}
		else
		{
			printf("ERROR; return code from pthread_join() is %d, thread %d\n",
					rc, i);
			return	-1;
		}
	}

	return	0;
}

void* thread_main(void* arg)
{
	int	i;
	double	result	=	0.0;

	printf("thread: %d, %d\n", (int) arg, getpid());

	while ( !done[(int)arg] )
	{
		for ( i = 0; i < 1000000; ++i )
		{
			result	=	result + (double)random();
		}
		printf("thread: %d, result = %e\n", (int)arg, result);
	}

	pthread_exit((void*) 0);
}

//	pthread2.c
#include 
#include 
#include 

pthread_t	threads[5];
int	done[5];

void* thread_main(void*);

//	http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Thread/Beginning/Example_pthread
//	$ gcc -o pthread2 pthread2.c -lpthread
//	result status will print -1
//	because pthread_cancel is used for forced termination
int main(void)
{
	int	i, rc, status;

	printf("pid = %d\n", getpid());

	for ( i = 0; i < 5; ++i )
	{
		done[i]	=	0;
		pthread_create(&threads[i], NULL, &thread_main, (void*)i);
		printf("%d, %d\n", i, threads[i]);
	}

	for ( i = 4; i >= 0; --i )
	{
		rc	=	pthread_cancel(threads[i]);	//	force termination
		if ( rc == 0 )
		{
			//	automatic termination
			rc	=	pthread_join(threads[i], (void**)&status);
			if ( rc == 0 )
			{
				printf("Completed join with thread %d status = %d\n", i,
						status);
			}
			else
			{
				printf("ERROR; return code from pthread_join() is %d, thread"
						"%d\n", rc, i);
				return	-1;
			}
		}
	}

	return	0;
}

void* thread_main(void* arg)
{
	int	i;
	double	result	=	0.0;

	printf("thread: %d, %d\n", (int)arg, getpid());

	while ( !done[(int)arg] )
	{
		for ( i = 0; i < 1000000; ++i )
		{
			result	=	result + (double)random();
		}
		printf("thread: %d, result = %e\n", (int)arg, result);
	}

	pthread_exit((void*) 0);
}

//	pthread3.c
#include 
#include 
#include 

pthread_t	threads[5];
int	done[5];

void* thread_main(void*);

int main(void)
{
	int	i, rc, status;

	printf("pid = %d\n", getpid());

	for ( i = 0; i < 5; ++i )
	{
		done[i]	=	0;
		pthread_create(&threads[i], NULL, &thread_main, (void*)i);
		printf("%d, %d\n", i, threads[i]);
	}

	for ( i = 4; i >= 0; --i )
	{
		done[i]	=	1;
		//	not need to use in detach thread because it's impossible to join a
		//	detached thread
		rc	=	pthread_join(threads[i], (void**)&status);	
		if ( rc == 0 )
		{
			printf("Completed join with thread %d status = %d\n", i, status);
		}
		else
		{
			printf("ERROR; return code from pthread_join() is %d, thread %d\n",
					rc, i);
			return	-1;
		}
	}

	return	0;
}

void* thread_main(void* arg)
{
	int	i;
	double	result	=	0.0;
	
	pthread_detach(pthread_self());	//	divide thread

	printf("thread: %d, %d\n", (int)arg, getpid());

	while ( !done[(int)arg] )
	{
		for ( i = 0; i < 1000000; ++i )
		{
			result	=	result + (double)random();
		}
		printf("thread: %d, result = %e\n", (int)arg, result);
	}

	pthread_exit((void*) 0);
}

//	pthread4.c
#include 
#include 
#include 

pthread_t	threads[5];
int	done[5];

void* thread_main(void*);

//	https://computing.llnl.gov/tutorials/pthreads
int main(void)
{
	int	i, rc, status;

	printf("pid = %d\n", getpid());

	for ( i = 0; i < 5; ++i )
	{
		done[i]	=	0;
		pthread_create(&threads[i], NULL, &thread_main, (void*)i);
		printf("%d, %d\n", i, threads[i]);
	}

	for ( i = 4; i >= 0; --i )
	{
		done[i]	=	1;
	}

	//	wait a moment till all threads end
	//	not using sleep, main will end, then created threads will do
	sleep(5);

	return	0;
}

void* thread_main(void* arg)
{
	int	i;
	double	result	=	0.0;
	
	pthread_detach(pthread_self());	//	divide thread

	printf("thread: %d, %d\n", (int)arg, getpid());

	while ( !done[(int)arg] )
	{
		for ( i = 0; i < 1000000; ++i )
		{
			result	=	result + (double)random();
		}
		printf("thread: %d, result = %e\n", (int)arg, result);
	}

	printf("thread %d terminated...\n", (int)arg);
	pthread_exit((void*) 0);
}
Comments