进程是分配资源的最小单位
线程是调度的最小单位
进程必须分配独立的地址空间,新的数据表、堆栈段和数据段
多线程遵循POSIX线程接口,不再标准的c库
需要头文件pthread.h
连接时需要libpthread.a-->gcc filename -lpthread
[size=x-large]创建线程[/size]
返回后先运行进程
示例;
[size=x-large]终止线程[/size]
在进程的任何一个线程调用exit或_exit, 整个进程都会终止
线程正常退出的方法:
1 从启动例程中返回
2 被另一个进程终止
3 自己调用pthread_exit函数
示例:
[size=x-large]线程等待[/size]
示例:
[size=x-large]线程标志[/size]
[size=x-large]资源的清理[/size]
调用点pthread_cleanup_push到调用点pthread_cleanup_pop之间的程序段中的终止动作
包括:调用pthread_exit()和异常终止, 不包括return
都将执行pthread_cleanup_push()所指定的清理函数
示例:
线程是调度的最小单位
进程必须分配独立的地址空间,新的数据表、堆栈段和数据段
多线程遵循POSIX线程接口,不再标准的c库
需要头文件pthread.h
连接时需要libpthread.a-->gcc filename -lpthread
[size=x-large]创建线程[/size]
#include <pthread.h>
int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg)
//tidp 线程ID
//attr 线程属性, 通常为空
//start_rtn 线程要执行的函数
//arg start_rtn的参数
返回后先运行进程
示例;
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *create(void *arg)
{
int *num;
num=(int *)arg;
printf("create parameter is %d \n",*num);
return (void *)0;
}
int main(int argc ,char *argv[])
{
pthread_t tidp;
int error;
int test=4;
int *attr=&test;
error=pthread_create(&tidp,NULL,create,(void *)attr);
if(error)
{
printf("pthread_create is created is not created ... \n");
return -1;
}
sleep(1);
printf("pthread_create is created ...\n");
return 0;
}
[size=x-large]终止线程[/size]
在进程的任何一个线程调用exit或_exit, 整个进程都会终止
线程正常退出的方法:
1 从启动例程中返回
2 被另一个进程终止
3 自己调用pthread_exit函数
#include <pthread.h>
void pthread_exit(void *rval_ptr)
//终止调用线程
//rval_ptr 线程退出返回值的指针
示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *create(void *arg)
{
printf("new thread is created ... \n");
return (void *)8;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
void *temp;
error = pthread_create(&tid, NULL, create, NULL);
printf("main thread!\n");
if( error )
{
printf("thread is not created ... \n");
return -1;
}
error = pthread_join(tid, &temp);
if( error )
{
printf("thread is not exit ... \n");
return -2;
}
printf("thread is exit code %d \n", (int )temp);
return 0;
}
[size=x-large]线程等待[/size]
#include <pthread.h>
void pthread_join(pthread_t tid, void **rval_ptr)
//阻塞调用线程, 知道指定的线程终止
//tid: 等待退出的线程id
//rval_ptr: 线程退出后返回值的指针
示例:
#include <pthread.h>
void *thr_fn1(void *arg)
{
printf("thread 1 returning\n");
return((void *)1);//返回值类型转换
}
void *thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
int
main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;//线程返回值的指针
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0)
err_quit("can't create thread 1: %s\n", strerror(err));
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0)
err_quit("can't create thread 2: %s\n", strerror(err));
err = pthread_join(tid1, &tret);//进程等待线程1返回
if (err != 0)
err_quit("can't join with thread 1: %s\n", strerror(err));
printf("thread 1 exit code %d\n", (int)tret);//输出线程1的返回值
err = pthread_join(tid2, &tret);
if (err != 0)
err_quit("can't join with thread 2: %s\n", strerror(err));
printf("thread 2 exit code %d\n", (int)tret);
exit(0);
}
[size=x-large]线程标志[/size]
#include <pthread.h>
pthread_t pthread_self(void)
//获取线程的thread identifier
[size=x-large]资源的清理[/size]
调用点pthread_cleanup_push到调用点pthread_cleanup_pop之间的程序段中的终止动作
包括:调用pthread_exit()和异常终止, 不包括return
都将执行pthread_cleanup_push()所指定的清理函数
void pthread_cleanup_push(void (*rtp)(void *), void *arg)
//功能: 将清理函数压入清理栈
//trn: 清理函数
//arg: 清理函数的指针
void pthread_cleanup_pop(int execute)
//功能: 将清理函数弹出清理栈
//execute 执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数
//非0-->执行(即使线程未推出,也执行), 0-->不执行
示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *clean(void *arg)
{
printf("cleanup :%s \n",(char *)arg);
return (void *)0;
}
void *thr_fn1(void *arg)
{
printf("thread 1 start \n");
pthread_cleanup_push( (void*)clean,"thread 1 first handler");
pthread_cleanup_push( (void*)clean,"thread 1 second hadler");
printf("thread 1 push complete \n");
if(arg)
{
return((void *)1);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)1;
}
void *thr_fn2(void *arg)
{
printf("thread 2 start \n");
pthread_cleanup_push( (void*)clean,"thread 2 first handler");
pthread_cleanup_push( (void*)clean,"thread 2 second handler");
printf("thread 2 push complete \n");
if(arg)
{
pthread_exit((void *)2);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
pthread_exit((void *)2);
}
int main(void)
{
int err;
pthread_t tid1,tid2;
void *tret;
err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
if(err!=0)
{
printf("error .... \n");
return -1;
}
err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
if(err!=0)
{
printf("error .... \n");
return -1;
}
err=pthread_join(tid1,&tret);
if(err!=0)
{
printf("error .... \n");
return -1;
}
printf("thread 1 exit code %d \n",(int)tret);
err=pthread_join(tid2,&tret);
if(err!=0)
{
printf("error .... ");
return -1;
}
printf("thread 2 exit code %d \n",(int)tret);
return 1;
}