多线程简介
- 为什么使用线程?
-
和进程相比,它是一种非常“节俭”的多任务操作方式。在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种“昂贵”的多任务工作方式。
-
线程间方便的通信机制。对不同进程来说,他们具有独立的数据空间,要进行数据的传递只能通过进程间通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其他线程所用,这不仅快捷,而且方便。
- 使用多线程有哪些优点?
- 运行于一个进程中的多个线程,他们之间使用相同的地址空间,而且线程间彼此切换所需要的时间也远远小于进程间切换所需要的时间。据统计,一个进程的开销大约是一个线程开销的30倍左右。
- 使多CPU系统更加有效。操作系统会保证当线程数大于CPU数目时,不同的线程运行于不同的CPU上。
- 改善程序结构。一个即长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
注意:编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a
线程编程
2.1 创建线程
#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系统的库,所以在进行编译的时候要加上-lpthread,例如#gcc filename -lpthread
举例代码如下:
#include <stdio.h>
#include <pthread.h>
void *myThread1(void)
{
int i;
for (i=0; i<3; i++)
{
printf("This is the 1st pthread,created by zieckey.\n");
sleep(1);//Let this thread to sleep 1 second,and then continue to run
}
}
void *myThread2(void)
{
int i;
for (i=0; i<3; i++)
{
printf("This is the 2st pthread,created by zieckey.\n");
sleep(1);
}
}
int main()
{
int i=0, ret=0;
pthread_t id1,id2;
/*创建线程1*/
ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
/*创建线程2*/
ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}
如何传递参数:
#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;
}
2.2 终止线程
如果进程中任何一个线程中调用exit或_exit,那么整个进程都会终止。线程的正常退出方式有:
- 线程从启动例程中返回
- 线程可以被另一个进程终止
- 线程自己调用pthread_exit函数
#include <pthread.h>
void pthread_exit(void * rval_ptr)
功能:终止调用线程
ravl_ptr:线程退出返回值的指针
2.3 线程等待
#include <pthread.h>
int pthread_join(pthread_t tid, void ** rval_ptr)
功能:阻塞调用线程,直到指定的线程终止
tid:等待退出的线程id
rval_ptr:线程退出的返回值的指针
举例:
#include <pthread.h>
#include <unistd.h>
#include <stdio.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, (void *)(i));
// 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;
}
2.4 线程标识
#include <pthread.h>
pthread_t pthread_self(void)
功能:获取调用线程的thread identifier
举例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> /*getpid()*/
void *create(void *arg)
{
printf("New thread .... \n");
printf("This thread's id is %u \n", (unsigned int)pthread_self());
printf("The process pid is %d \n",getpid());
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
printf("Main thread is starting ... \n");
error = pthread_create(&tid, NULL, create, NULL);
if(error)
{
printf("thread is not created ... \n");
return -1;
}
printf("The main process's pid is %d \n",getpid());
sleep(1);
return 0;
}
线程终止有两种情况:正常终止和非正常终止。线程主动调用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:不执行
举例:
#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);/*参数改为1试试*/
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;
}
输出结果:
thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
clean up: thread 2 second handler /*注意顺序,栈*/
clean up: thread 2 first handler