linux操作系统下的多线程编程详细解析Word文件下载.docx
- 文档编号:21671648
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:17
- 大小:19.87KB
linux操作系统下的多线程编程详细解析Word文件下载.docx
《linux操作系统下的多线程编程详细解析Word文件下载.docx》由会员分享,可在线阅读,更多相关《linux操作系统下的多线程编程详细解析Word文件下载.docx(17页珍藏版)》请在冰豆网上搜索。
操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。
3)改善程序结构。
一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
下面我们先来尝试编写一个简单的多线程程序。
简单的多线程编程
Linux系统下的多线程遵循POSIX线程接口,称为pthread。
编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。
顺便说一下,Linux下pthread的实现是通过系统调用clone()来实现的。
clone()是Linux所特有的系统调用,它的使用方式类似fork,关于clone()的详细情况,有兴趣的读者可以去查看有关文档说明。
下面我们展示一个最简单的多线程程序pthread_create.c。
一个重要的线程创建函数原型:
#include<
pthread.h>
intpthread_create(pthread_t*restricttidp,constpthread_attr_t*restrictattr,void*(*start_rtn)(void),void*restrictarg);
返回值:
若是成功建立线程返回0,否则返回错误的编号
形式参数:
pthread_t*restricttidp要创建的线程的线程id指针
constpthread_attr_t*restrictattr创建线程时的线程属性
void*(start_rtn)(void)返回值是void类型的指针函数
void*restrictarg
start_rtn的行参
例程1:
功能:
创建一个简单的线程
程序名称:
pthread_create.c
/********************************************************************************************
**
Name:
pthread_create.c
UsedtostudythemultithreadprogramminginLinuxOS
Author:
zeickey
Date:
2006/9/16
Copyright(c)2006,AllRightsReserved!
*********************************************************************************************/
stdio.h>
void*myThread1(void)
{
inti;
for(i=0;
i<
100;
i++)
{
printf("
Thisisthe1stpthread,createdbyzieckey.\n"
);
sleep
(1);
//Letthisthreadtosleep1second,andthencontinuetorun
}
}
void*myThread2(void)
Thisisthe2stpthread,createdbyzieckey.\n"
intmain()
inti=0,ret=0;
pthread_tid1,id2;
ret=pthread_create(&
id2,NULL,(void*)myThread1,NULL);
if(ret)
Createpthreaderror!
\n"
return1;
id2,NULL,(void*)myThread2,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return0;
我们编译此程序:
#gccpthread_create.c-lpthread
因为pthread的库不是linux系统的库,所以在进行编译的时候要加上-lpthread,否则编译不过,会出现下面错误
thread_test.c:
在函数‘create’中:
7:
警告:
在有返回值的函数中,程序流程到达函数尾
/tmp/ccOBJmuD.o:
Infunction`main'
:
(.text+0x4f):
对‘pthread_create’未定义的引用
collect2:
ld返回1
运行,我们得到如下结果:
#./a.out
Thisisthe1stpthread,createdbyzieckey.
Thisisthe2stpthread,createdbyzieckey.
....
两个线程交替执行。
此例子介绍了创建线程的方法。
下面例子介绍向线程传递参数。
例程2:
向新的线程传递整形值
pthread_int.c
Passaparametertothethread.
unistd.h>
void*create(void*arg)
int*num;
num=(int*)arg;
createparameteris%d\n"
*num);
return(void*)0;
intmain(intargc,char*argv[])
pthread_ttidp;
interror;
inttest=4;
int*attr=&
test;
error=pthread_create(&
tidp,NULL,create,(void*)attr);
if(error)
pthread_createiscreatedisnotcreated...\n"
return-1;
pthread_createiscreated...\n"
编译方法:
gcc-lpthreadpthread_int.c-Wall
执行结果:
createparameteris4
pthread_createiscreatedis
created...
例程总结:
可以看出来,我们在main函数中传递的整行指针,传递到我们新建的线程函数中。
在上面的例子可以看出来我们向新的线程传入了另一个线程的int数据,线程之间还可以传递字符串或是更复杂的数据结构。
例程3:
程序功能:
向新建的线程传递字符串
pthread_string.c
Passa‘char*‘parametertothethread.
char*name;
name=(char*)arg;
Theparameterpassedfrommainfunctionis%s
\n"
name);
intmain(intargc,char*argv[])
char*a="
zieckey"
;
tidp,NULL,create,(void*)a);
if(error!
=0)
pthreadisnotcreated.\n"
pthreadiscreated...\n"
}
gcc-Wallpthread_string.c-lpthread
Theparameterpassedfrommainfunctioniszieckey
pthreadiscreated...
可以看出来main函数中的字符串传入了新建的线程中。
例程4:
pthread_struct.c
stdlib.h>
structmenber
inta;
char*s;
};
structmenber*temp;
temp=(structmenber*)arg;
menber->
a=%d
temp->
a);
s=%s
s);
intmain(intargc,char*argv[])
structmenber*b;
b=(structmenber*)malloc(sizeof(structmenber));
b->
a=4;
s="
error=pthread_create(&
tidp,NULL,create,(void*)b);
if(error)
phreadisnotcreated...\n"
pthreadiscreated...\n"
gcc-Wallpthread_struct.c-lpthread
a=4
s=zieckey
pthreadiscreated...
可以看出来main函数中的一个结构体传入了新建的线程中。
线程包含了标识进程内执行环境必须的信息。
他集成了进程中的所有信息都是对线程进行共享的,包括文本程序、程序的全局内存和堆内存、栈以及文件描述符。
例程5:
程序目的:
验证新建立的线程可以共享进程中的数据
pthread_share.c
pthread_share_data.c
staticinta=4;
newpthread...\n"
a=%d
a);
a=5;
tidp,NULL,create,NULL);
newthreadisnotcreate...\n"
newthreadiscreated...\n"
gcc-Wallpthread_share_data.c-lpthread
newpthread...
a=5
newthreadiscreated...
可以看出来,我们在主线程更改了我们的全局变量a的值的时候,我们新建立的线程则打印出来了改变的值,可以看出可以访问线程所在进程中的数据信息。
Linux操作系统下的多线程编程详细解析
(2)
2、线程的终止
如果进程中任何一个线程中调用exit,_Exit,或者是_exit,那么整个进程就会终止,
与此类似,如果信号的默认的动作是终止进程,那么,把该信号发送到线程会终止进程。
线程的正常退出的方式:
(1)线程只是从启动例程中返回,返回值是线程中的退出码
(2)线程可以被另一个进程进行终止
(3)线程自己调用pthread_exit函数
两个重要的函数原型:
voidpthread_exit(void*rval_ptr);
/*rval_ptr线程退出返回的指针*/
intpthread_join(pthread_tthread,void**rval_ptr);
/*成功结束进程为0,否则为错误编码*/
例程6
线程正常退出,接受线程退出的返回码
pthread_exit.c
Name:
UsedtostudythemultithreadprogramminginLinuxOS
Aexampleshowingathreadtoexitandwithareturncode.
Author:
Date:
Copyright(c)2006,AllRightsReserved!
*********************************************
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux 操作系统 多线程 编程 详细 解析