多线程
为什么有了进程,还要引入线程呢?
使用多线程到底有哪些好处?
多线程优点:
使用线程的理由之一是:和进程相比,它是一种“节俭”的多任务操作方式。在linux系统下,启动一个新的进程必须分配给他独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种“昂贵的”多任务工作方式。
运行于一个进程中的多个线程,他们之间使用相同的地址空间,而且线程间彼此切换所用的时间也远远小于进程间切换所用的时间。据统计,一个进程的开销大约是一个线程开销的30倍左右。
使用多线程理由之二:
线程间方便的通信机制。对不同进程来说,他们具有独立的数据空间,要进行数据传递只能通过进程间通信的方式进行,这种方式不仅耗时而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。
除了以上优点之外,多线程作为一种多任务、并发的工作方式,有如下优点:
使多CPU系统更加有效,操作系统会保证当线程数不大于CPU数目时,不同的线程运行在不同的CPU上。
改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或者半独立的部分,这样的程序会有利于理解和修改。
创建线程
#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的参数
编译
因为pthread的库不是LINUX系统库,所以在编译的时候要加上 -lphread
#gcc filename -lphread -o 。。。
创建一个线程实例:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 通过pthread_join阻塞等待,直至相应线程结束。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
void *thread(void *str)
{
int i;
for (i = 0; i < 3; ++i)
{
sleep(2);
printf( "This in the thread : %d\n" , i );
}
return NULL;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int i;
/*创建线程并执行线程执行函数*/
int ret = pthread_create(&pth, NULL, thread, NULL);
printf("The main process will be to run,but will be blocked soon\n");
/*阻塞等待线程退出*/
pthread_join(pth, NULL);
printf("thread was exit\n");
for (i = 0; i < 3; ++i)
{
sleep(1);
printf( "This in the main : %d\n" , i );
}
return 0;
}
创建一个线程并向线程传递一个整形参数
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 通过pthread_join阻塞等待,直至相应线程结束。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
void *thread(void *str)
{
int *num;
num=(int *)arg;
printf("create parameter is %d\n",*num);
return (void*)0;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int error;
int test=4;
int *attr=&test;
/*创建线程并执行线程执行函数*/
error = pthread_create(&pth, NULL, thread, (void *)attr);
if(error)
{
printf("pthread_create is created is not create...\n");
return -1;
}
sleep(1);
printf("pthread_create is created\n");
return 0;
}
创建一个线程并向线程里面传递一个结构体作为参数实例:
/**********************************************************
*实验要求: 在程序中创建一个线程,并向该线程处理函数传递一个结构体作为
* 参数。
*功能描述: 通过pthread_create创建一个线程并传入一个结构体参数,再
* 在线程中接收到这个参数并把参数内容打印出来。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
struct menber
{
int a;
char *s;
};
/*
* 线程执行函数
* */
void *create(void *arg)
{
struct menber *temp;
temp=(struct menber *)arg;
printf("menber->a = %d \n",temp->a);
printf("menber->s = %s \n",temp->s);
return (void *)0;
}
/*
* 程序入口
* */
int main(int argc,char *argv[])
{
pthread_t tidp;
int error;
struct menber *b;
/*为结构体指针b分配内存并赋值*/
b=(struct menber *)malloc( sizeof(struct menber) );
b->a = 4;
b->s = "zieckey";
/*创建线程并运行线程执行函数*/
error = pthread_create(&tidp, NULL, create, (void *)b);
if( error )
{
printf("phread is not created...\n");
return -1;
}
sleep(1); //进程睡眠一秒使线程执行完后进程才会结束
printf("pthread is created...\n");
return 0;
}
线程共享进程数据的程序实例:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 创建的线程和创建线程的进程是共享数据段中的数据。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
int a=1; /*存放在数据段当中的数据*/
void *thread(void *str)
{
printf("new pthread ...\n");
printf("a=%d \n",a);
a++;
return (void *)0;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int error;
printf("in main 1: a=%d",a);
/*创建线程并执行线程执行函数*/
error = pthread_create(&pth, NULL, thread, NULL);
if(error!=0)
{
printf("new pthread is not created ...\n");
return -1;
}
sleep(3);
printf("in main 2: a=%d",a);
printf("new thread is create...\n");
return 0;
}
运行结果如下:
由以上程序可知,创建的线程和创建线程的进程是共享数据段的。
若在main函数之内定义一个同名的变量,程序如下:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 创建的线程和创建线程的进程是共享数据段中的数据。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
int a=1; /*存放在数据段(堆)当中的数据*/
void *thread(void *str)
{
printf("new pthread ...\n");
printf("a=%d \n",a);
a++;
return (void *)0;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int error;
int a=5; /*存放在栈里面的数据*/
printf("in main 1: a=%d",a);
/*创建线程并执行线程执行函数*/
error = pthread_create(&pth, NULL, thread, NULL);
if(error!=0)
{
printf("new pthread is not created ...\n");
return -1;
}
sleep(3);
printf("in main 2: a=%d",a);
printf("new thread is create...\n");
return 0;
}
程序运行结果如下:
由以上结果可知,线程会共享创建线程的进程中的内容,并且共享的是数据段的内容(int a=1),而不能是栈里面的内容(int a=5)。若将程序中int a=1删除掉,程序编译无法通过。
终止线程
如果进程中任何一个线程中调用exit或_exit,那么整个进程都会终止,线程的正常终止方式有:
(1) 线程从启动例程中返回return。
(2) 线程可以被另一个进程终止(kill)。
(3) 线程自己调用pthread_exit函数。
#inlucde<pthread.h>
void pthread_exit(void *rval_ptr)
功能:终止调用线程。
Rval_ptr:线程退出返回值的指针。
线程终止实例:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 线程终止方式,return。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
void *thread(void *str)
{
printf( "new thread is created \n" );
return (void *)8;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int error;
void *temp;
/*创建线程并执行线程执行函数*/
error = pthread_create(&pth, NULL, thread, NULL);
printf("The main process will be to run,but will be blocked soon\n");
/*阻塞等待线程退出*/
if(error!=0)
{
printf("new pthread is not created ...\n");
return -1;
}
error=pthread_join(pth, &temp);
if(error!=0)
{
printf("thread is not exit ...\n");
return -2;
}
printf("thread is exit code %d \n",(int)temp);
return 0;
}
运行结果如下:
线程等待
#include<pthread.h>
int pthread_join(pthread_t tid,void **rval_ptr)
功能:阻塞调用线程,直到指定的线程终止。
Tid:等待退出的县城ID
Rval_ptr:线程退出的返回值的指针。
线程等待实例:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 通过pthread_join阻塞等待,直至相应线程结束。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
void *thread(void *str)
{
int i;
for (i = 0; i < 3; ++i)
{
sleep(2);
printf( "This in the thread : %d\n" , i );
}
return NULL;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int i;
/*创建线程并执行线程执行函数*/
int ret = pthread_create(&pth, NULL, thread, NULL);
printf("The main process will be to run,but will be blocked soon\n");
/*阻塞等待线程退出*/
pthread_join(pth, NULL);
printf("123\n");
for (i = 0; i < 3; ++i)
{
sleep(1);
printf( "This in the main : %d\n" , i );
}
return 0;
}
程序运行结果如下:
若将pthread_join()等待函数去掉,代码及程序运行结果如下:
/**********************************************************
*实验要求: 在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述: 通过pthread_join阻塞等待,直至相应线程结束。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/*
* 线程的执行函数
* */
void *thread(void *str)
{
int i;
for (i = 0; i < 3; ++i)
{
sleep(2);
printf( "This in the thread : %d\n" , i );
}
return NULL;
}
/*
* 程序入口
* */
int main()
{
pthread_t pth;
int i;
/*创建线程并执行线程执行函数*/
int ret = pthread_create(&pth, NULL, thread, NULL); /*一但创建此线程,线程会处于就绪态,就绪不等于执行态,所以程序依然往下执行*/
printf("The main process will be to run,but will be blocked soon\n");
/*阻塞等待线程退出*/
// pthread_join(pth, NULL);
printf("123\n");
for (i = 0; i < 3; ++i)
{
sleep(1);
printf( "This in the main : %d\n" , i );
}
return 0;
}
线程标识:
#include<pthread.h>
pthread_t pthread_self(void)
功能:获取调用线程的thread indentifer
线程的清除
线程终止有两种情况:正常终止和非正常终止。线程主动调用pthread_exit或者从线程函数中return都将使得线程正常终止,这是可以预见的退出方式;非正常终止是线程在其他线程的干预下,或者由于自身运行错误而退出,这种退出方式不可预见的。
在非正常或者正常终止条件下,如何释放资源,是一个必须考虑的问题。
从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的程序段中的终止动作(包括调用pthread_exit()和异常终止,不包括return)都将执行pthread_cleanup_push()所指定的清理函数。
#include<pthread.h>
void pthread_cleanup_push(void(*rtn)(void *),void *arg)
功能:将清除函数压入栈
rtn:清除函数
arg:清除函数参数
#include<pthread.h>
void pthread_cleanup_pop(int execute)
功能:将清除函数弹出清除栈
参数:execute执行到pthread_cleanup_pop时是否在弹出清理函数的同时执行该函数,非0:执行,0:不执行。
进程清除程序如下:
/**********************************************************
*实验要求: 在程序中创建一个线程,使用线程API对该线程进行清理工作。
*功能描述: 创建线程,并在其中使用函数pthread_cleanup_push和函数
* pthread_cleanup_pop,验证这两个清理函数的效果。
*日 期: 2010-9-17
*作 者: 国嵌
**********************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
/*
* 线程清理函数
* */
void *clean(void *arg)
{
printf("cleanup :%s\n",(char *)arg);
return (void *)0;
}
/*
* 线程1的执行函数
* */
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); //线程运行到这里会结束,后面的代码不会被运行。由于是用return退出,所以不会执行线程清理函数。
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)1;
}
/*
* 线程2的执行函数
* */
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_exit退出,所以会执行线程清理函数。执行的顺序是先压进栈的后执行,即后进先出。
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
pthread_exit((void *)2);
}
/*
* 程序入口
* */
int main(void)
{
int err;
pthread_t tid1,tid2;
void *tret;
/*创建线程1并执行线程执行函数*/
err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
if(err!=0)
{
printf("error .... \n");
return -1;
}
/*创建线程2并执行线程执行函数*/
err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
if(err!=0)
{
printf("error .... \n");
return -1;
}
/*阻塞等待线程1退出,并获取线程1的返回值*/
err=pthread_join(tid1,&tret);
if(err!=0)
{
printf("error .... \n");
return -1;
}
printf("thread 1 exit code %d \n",(int)tret);
/*阻塞等待线程2退出,并获取线程2的返回值*/
err=pthread_join(tid2,&tret);
if(err!=0)
{
printf("error .... ");
return -1;
}
printf("thread 2 exit code %d \n",(int)tret);
return 1;
}
程序运行结果如下: