日期:2014-05-16  浏览次数:20713 次

linux多线程例子

一.基本功能

1.最简单例子--创建线程

/*
*
*	创建线程
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1(void *arg)
{
	printf("this thread1!\n");
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	ret = pthread_create(&tid,NULL,thread1,NULL);
	sleep(4);
	return 0;
}

2.传递简单参数

/*
*
*	线程传递参数
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1(void *arg)
{
	char *prm=NULL;
	prm=(char*)arg;
	printf("arg=%s\n",prm);
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	ret = pthread_create(&tid,NULL,thread1,(void*)argv[1]);
	sleep(4);
	return 0;
}

3.传递结构体参数

/*
*
*	线程传递结构体
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

typedef struct prm{
	int in;
	char ch[255];
	int *p;
}oz;

void *thread1(void *arg)
{
	oz *prm2 = NULL;
	prm2=(oz*)arg;
	
	printf(" prm:in=%d\n prm:ch=%s\n prm:p=%d\n",prm2->in,prm2->ch,*(prm2->p));
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	oz *prm1=malloc(sizeof(oz));
	
	prm1->in=3;
	sprintf(prm1->ch,"hello world!");
	prm1->p=malloc(sizeof(prm1->p));
	*(prm1->p)=123456;
	
	ret = pthread_create(&tid,NULL,thread1,(void*)prm1);
	
	sleep(4);
	
	return 0;
}

4.主线程等待子线程结束

/*
*
*	主线程等待子线程结束
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1(void *arg)
{
	char *prm=NULL;
	prm=(char*)arg;
	printf("arg=%s\n",prm);
	sleep(5);
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	ret = pthread_create(&tid,NULL,thread1,(void*)argv[1]);

	pthread_join(tid,NULL);
	return 0;
}

5.获取线程id

/*
*
*	获取线程id
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1(void *arg)
{
	char *prm=NULL;
	prm=(char*)arg;
	printf("thread1's id=%lu\n",pthread_self());
	sleep(5);
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	ret = pthread_create(&tid,NULL,thread1,(void*)argv[1]);
	printf("main thread's id=%lu\n",pthread_self());
	printf("child thread's id=%lu\n",tid);
	pthread_join(tid,NULL);
	return 0;
}

6.子线程结束释放资源

/*
*
*	子线程结束释放资源
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1(void *arg)
{
	char *prm=NULL;
	prm=(char*)arg;
	sleep(5);
	pthread_detach(pthread_self());
}

int main(int argc,char **argv)
{
	int ret;
	pthread_t tid;
	ret = pthread_create(&tid,NULL,thread1,(void*)argv[1]);
	pthread_join(tid,NULL);
	return 0;
}

7.创建多个子线程;子线程退出;发送退出信号给子线程

/*
*
*	创建多个子线程;子线程退出;发送退出信号给子线程
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread2(void *arg)
{
	char *prm=NULL;
	int i=5;
	prm=(char*)arg;
	while(1){
		printf("thread2:%s\n",prm);
		sleep(1);
	}
}


void *thread1(void *arg)
{
	char *prm=NULL;
	int i=5;
	prm=(char*)arg;
	while(i--){
		printf("thread1:%s\n",prm);
		sleep(1);
	}
	pthread_detach(pthread_self());
	pthread_exit("omg!");
}

int main(int argc,char **argv)

{
	int ret;
	pthread_t tid1,tid2;
	void *join_ret;
	
	ret = pthread_create(&tid1,NULL,thread1,(void*)argv[1]);
	pthread_join(tid1,&join_ret);
	printf("thread1 exit return:%s\n",(char *)join_ret);
	ret = pthread_create(&tid2,NULL,thread2,(void*)argv[2]);
	sleep(5);
	if(!pthread_cancel(tid2))
		printf("cancel pthread2\n");
	pthread_join(tid2,NULL);
	return 0;
}

8.一些错误的判断及处理

/*
*
*	错误的判断及处理
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

void *thread2(void