linux 多线程API

运行于一个进程中的多个线程,它们之间使用相同的地址空间,而且线程间彼此切换所需的时间远远小于进程间切换所需要的时间。据统计,一个进程的开销大约是一个线程开销的30倍左右。同一进程下的线程直接共享数据空间。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
函数功能:创建线程。
thread:为指向线程标识符的指针。(存放线程标识符)
attr:线程属性。(通常为空,为空代表使用默认属性。默认的属性为非绑定、非分离、缺省 1M 的堆栈、与父进程同样级别的优先级,设置需要调用 pthread_attr_init 函数)
start_routine:线程要执行的函数。
arg:start_routine 函数的参数。
返回值:创建线程成功时,函数返回0,若不为0则说明创建线程失败。


void pthread_exit(void *retval);
退出线程。
retval:调用线程的返回值,可由 pthread_join 来检索获取。(类似 return)


int pthread_join(pthread_t thread, void **retval);
以阻塞的方式等待 thread 指定的线程结束。当函数返回时,被等待线程如果已经结束,那么该函数会立即返回。
thread : 线程标识符,即线程id,标识唯一线程。
retval : 用户定义的指针,用来存储被等待线程的返回值。
返回值:0代表成功。 失败,返回的则是错误号。

pthread_t pthread_self(void);
获取线程的id。

void pthread_cleanup_push(void (*routine)(void *), void *arg);
将清除函数压入清除栈
routine :清除函数。
arg:清除函数的参数。

void pthread_cleanup_pop(int execute);
将清除函数弹出清除栈
execute :决定执行到 pthread_cleanup_pop 时是否在弹出清理函数的同时执行该函数。值为 0 不执行,非 0 执行。


从 pthread_cleanup_push 的调用点到 pthread_cleanup_pop 之间的程序段中的终止动作(包括调用 pthread_exit 和 异常终止,不包括 return)都将执行 pthread_cleanup_push 所指的清理函数。

示例代码:

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

void pthread_clean(void *arg){
	printf("clean fun from %s\n",(char *)arg);
}

void *thr_fun1(void *arg){
	pthread_cleanup_push(pthread_clean,"thr_fun1");
	printf("come into the fun1\n");
	printf("fun1 from progress %d\n",getpid());
	printf("thread1 id is %lu\n",pthread_self());
	if(!arg)
		pthread_exit((void *)1);
	pthread_cleanup_pop(0);
}

void *thr_fun2(void *arg){
	pthread_cleanup_push(pthread_clean,"thr_fun2");
	printf("come into the fun2\n");
	printf("fun2 from progress %d\n",getpid());
	printf("thread2 id is %lu\n",pthread_self());
	if(!arg)
		pthread_exit((void *)2);
	pthread_cleanup_pop(1);
}

void *thr_fun3(void *arg){
	pthread_cleanup_push(pthread_clean,"thr_fun3");
	printf("come into the fun3\n");
	printf("fun3 from progress %d\n",getpid());
	printf("thread3 id is %lu\n",pthread_self());
	if(!arg)
		return (void *)3;
	pthread_cleanup_pop(1);
}

int main(int argc, char **argv){
	
	pthread_t pth_id1,pth_id2,pth_id3;
	void *a1,*a2,*a3;
	int res;
	if((res = pthread_create(&pth_id1,0,thr_fun1,NULL)))
		fprintf(stderr,"%s\n",strerror(res));

	if((res = pthread_create(&pth_id2,0,thr_fun2,NULL)))
		fprintf(stderr,"%s\n",strerror(res));

	if((res = pthread_create(&pth_id3,0,thr_fun3,NULL)))
		fprintf(stderr,"%s\n",strerror(res));
		
	pthread_join(pth_id1,&a1);
	pthread_join(pth_id2,&a2);
	pthread_join(pth_id3,&a3);
	printf("thr_fun1 exit code %d\n",(int)a1);
	printf("thr_fun1 exit code %d\n",(int)a2);
	printf("thr_fun1 exit code %d\n",(int)a3);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值