多线程编程

1.概念
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。

2.特点
1)同一进程的线程共享本进程的地址空间,而进程之间则是独立的地址空间。
2)进程退出,进程中所有的线程全部退出。
3)一个进程崩溃后,不会对其他的进程产生影响;但一个线程崩溃后,整个进程就崩溃,所以多进程比多线程健壮。
4)线程不可能完全代替进程。
5)线程拥有自己独立的属性。
6) 线程的开心比较低,实际上是函数的切换。
7)线程的通信简单:全局变量。
8)linux线程支持基于库的,库的优点是可以跨平台。

3.相关函数
1)函数原型:int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,
void *(start_rtn)(void),void *arg);

功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。

参数
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。(一般为系统自动设置,写NULL即可)
第三个参数是线程运行函数的起始地址。(调用的函数名)
最后一个参数是运行函数的参数。()

返回值
若线程创建成功,则返回0。若线程创建失败,则返回出错编号,并且*thread中的内容是未定义的。

2)函数原型: int pthread_join(pthread_t thread, void **retval);

功能:用来等待一个线程的结束,线程间同步的操作。以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。

参数
thread: 线程标识符,即线程ID,标识唯一线程。
retval: 用户定义的指针,用来存储被等待线程的返回值。

返回值 : 0代表成功。 失败,返回的则是错误号。

3)函数原型:void pthread_exit(void* retval);

功能:终止调用它的线程并返回一个指向某个对象的指针。

4)函数原型:int pthread_cancel(pthread_t thread)

功能:发送终止信号给thread线程,如果成功则返回0,否则为非0值。

4.线程退出函数的区别:pthread_exit–return–exit–pthread_cancel
1)子线程中调用pthread_exit或return只会使该线程退出,不会影响其他线程。但用exit会导致进程结束。
2)主线程中调用pthread_exit函数只会使主函数所在的线程(进程的主线程)退出;如果是return,编译器将使其调用进程退出的代码(如exit),
从而导致进程及其所有线程结束运行。
3)被动退出(终止同一进程中的某一线程):pthread_cancel(被终止的线程任务中,必须发生系统调用,否则终止不了,终止后不影响其他线程运行)。

主线程与俩个子线程同时运行

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void* task1(void *arg){
	int i = 10;
	while(i--){
		printf("task111 i=%d\n",i);
		sleep(1);
	}
	return NULL;
}

void* task2(void *arg){
	int i = 10;
	while(i--){
		printf("task222 i=%d\n",i);
		sleep(1);
	}
	return NULL;
}

int main(){
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,NULL) != 0){
		perror("pthread_creat error\n");
		exit(1);
	}
	printf("create thread successful! tid1=%ld\n",tid1);

	if(pthread_create(&tid2,NULL,task2,NULL) != 0){
			perror("pthread_creat error\n");
			exit(1);
		}
		printf("create thread successful! tid2=%ld\n",tid2);

		sleep(12); //避免主线程先退出,导致调用return使所有线程都退出
	return 0;
}

**注:main为主线程。

线程之间的通讯

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

char buf[100];

void* task111(void *arg){
	while(1){
		scanf("%s",buf);
	}
}

void* task222(void *arg){
	while(1){
		if(strlen(buf) == 0){
			sleep(1);
			continue;
		}
		printf("receive:%s\n",buf);
		memset(buf,0,sizeof(buf));
	}
}

int main(){
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task111,NULL) != 0){
		perror("pthread_creat error\n");
		exit(1);
	}
	printf("create thread successful! tid1=%ld\n",tid1);

	if(pthread_create(&tid2,NULL,task222,NULL) != 0){
			perror("pthread_creat error\n");
			exit(1);
		}
		printf("create thread successful! tid2=%ld\n",tid2);

		while(1){
			sleep(1);
		}
	return 0;
}

多线程互斥

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define N 10000

void* task1(void * arg)
{
	int i = 0;
    int fd = *((int*)arg);
	//write

	while(i++ < N){
		write(fd,"11111",5);
		write(fd,"22222\n",6);
	}
	return NULL;
}

void* task2(void * arg)
{
	int i = 0;
     int fd = *((int*)arg);
		//write

		while(i++ < N){
			write(fd,"33333",5);
			write(fd,"44444\n",6);
		}
		return NULL;
	}


int main(){


	pthread_t tid1,tid2;

	int fd;

	fd = open("/home/ben/c.d/t1.txt",O_RDWR|O_CREAT|O_APPEND,0755);
	if(fd == -1){
		printf("open the file fail");
		exit(1);
	}else{
		printf("open successful!\n");
	}

	if(pthread_create(&tid1,NULL,task1,(void *)(&fd)) == -1){
		perror("pthread_create error!");
		exit(1);
	}
	printf("create thread successful!  tid1=%ld\n",tid1);

	if(pthread_create(&tid2,NULL,task2,(void *)(&fd)) == -1){
		perror("pthread_create error!");
		exit(1);
	}
	printf("create thread successful!tid2=%ld\n",tid2);

	while(1){
		sleep(1);
	}
	return 0;
}

线程退出,join

1.线程退出状态的获得
pthread_join可以获取pthread_exit函数结束时返回的值。
pthread_exit的参数为一个一级指针,pthread_join要用二级指针容纳。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>


 int retval;
void* task(void * arg)
{
    int i = 0;
		while(i < 10){
			printf("task3:%d\n",i++);
			sleep(1);
		}
		retval = 20;
		pthread_exit(&retval);
	}


int main(){
		int	fd = open("/home/ben/c.d/test2.txt",O_RDWR|O_CREAT|O_APPEND,0755);
			if(fd == -1){
				perror("open error!\n");
			}

		pthread_t tid;
		int *retval2;

	if(pthread_create(&tid,NULL,task,NULL)){
		perror("pthread_create error!");
		exit(1);
	}
	printf("create thread successful!  tid1=%ld\n",tid);

	int i = 0;
	while(i < 5){
		printf("main:%d\n",i++);
		sleep(1);
	}
	pthread_join(tid,&retval2);
	printf("continue to run... retval:%d\n",*retval2);

	return 0;
}

2.线程退出线程处理函数
概念:线程可以安排它退出时需要调用的函数,这样的函数称为线程清理处理程序,线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说它们的执行顺序与它们注册时的顺序相反。
相当于压栈和出栈
void pthread_cleanup_push(void (*rtn)(void *),void *arg);
第一个参数为处理程序入口地址(即调用函数名)
第二个参数为传递给处理函数的参数
void pthread_cleanup_pop(int execute);

注:
1)如果线程只是由于简单的返回而终止的,则清除函数不会被调用。
2)如果pthread_cleanup_pop被传递0参数,则清除函数不会被调用,但是会清除处于栈顶的清理函数。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>


void cleanup1(){
	printf("destroy1...\n");
}

void cleanup2(){
	printf("destroy2...\n");
}

void* task(void * arg)
{
	pthread_cleanup_push(cleanup1,NULL);
	pthread_cleanup_push(cleanup2,NULL);
    int i = 0;
		while(i < 10){
			printf("task3:%d\n",i++);
			sleep(1);
		}
		pthread_exit(NULL);
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	}


int main(){
		int	fd = open("/home/ben/c.d/test2.txt",O_RDWR|O_CREAT|O_APPEND,0755);
			if(fd == -1){
				perror("open error!\n");
			}

		pthread_t tid;

	if(pthread_create(&tid,NULL,task,NULL)){
		perror("pthread_create error!");
		exit(1);
	}
	printf("create thread successful!  tid1=%ld\n",tid);

	pthread_join(tid,NULL);

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值