Linux多线程实例解析Word文件下载.docx
《Linux多线程实例解析Word文件下载.docx》由会员分享,可在线阅读,更多相关《Linux多线程实例解析Word文件下载.docx(16页珍藏版)》请在冰豆网上搜索。
**Date:
2006/9/16
**Copyright(c)2006,AllRightsReserved!
*****************************************************************************/
#include<
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(&
id1,NULL,(void*)myThread1,NULL);
//创建成功返回0,否则非0
if(ret)//表达式值为0,按“假”处理;
非0时按“真”处理。
Createpthreaderror!
\n"
return1;
id2,NULL,(void*)myThread2,NULL);
if(ret)
pthread_join(id1,NULL);
//等待线程1结束
pthread_join(id2,NULL);
//等待线程2结束
return0;
我们编译此程序:
#gccpthread_create.c-lpthread
因为pthread的库不是linux系统的库,所以在进行编译的时候要加上-lpthread,否则编译不过,会出现下面错误
thread_test.c:
在函数‘create’中:
7:
警告:
在有返回值的函数中,程序流程到达函数尾
/tmp/ccOBJmuD.o:
Infunction`main'
:
thread_test.c:
(.text+0x4f):
对‘pthread_create’未定义的引用
collect2:
ld返回1
运行,我们得到如下结果:
#./a.out
Thisisthe1stpthread,createdbyzieckey.
Thisisthe2stpthread,createdbyzieckey.
....
两个线程交替执行。
此例子介绍了创建线程的方法。
下面例子介绍向线程传递参数。
例程2:
功能:
向新的线程传递整形值
程序名称:
pthread_int.c
/********************************************************************************************
**Name:
pthread_int.c
**UsedtostudythemultithreadprogramminginLinuxOS
**Passaparametertothethread.
**Author:
zeickey
**Date:
2006/9/16
**Copyright(c)2006,AllRightsReserved!
*********************************************************************************************/
unistd.h>
void*create(void*arg)
{
int*num;
num=(int*)arg;
printf("
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;
}
sleep
(1);
pthread_createiscreated...\n"
return0;
编译方法:
gcc-lpthreadpthread_int.c-Wall
执行结果:
createparameteris4
pthread_createiscreatediscreated...
例程总结:
可以看出来,我们在main函数中传递的整行指针,传递到我们新建的线程函数中。
在上面的例子可以看出来我们向新的线程传入了另一个线程的int数据,线程之间还可以传递字符串或是更复杂的数据结构。
例程3:
程序功能:
向新建的线程传递字符串
程序名称:
pthread_string.c
/********************************************************************************************
**Name:
**UsedtostudythemultithreadprogramminginLinuxOS
**Passa‘char*‘parametertothethread.
**Author:
**Date:
**Copyright(c)2006,AllRightsReserved!
*********************************************************************************************/
#include<
void*create(void*arg)
{
char*name;
name=(char*)arg;
printf("
Theparameterpassedfrommainfunctionis%s\n"
name);
return(void*)0;
}
intmain(intargc,char*argv[])
char*a="
zieckey"
;
interror;
pthread_ttidp;
error=pthread_create(&
tidp,NULL,create,(void*)a);
if(error!
=0)
pthreadisnotcreated.\n"
return-1;
sleep
(1);
pthreadiscreated...\n"
return0;
编译方法:
gcc-Wallpthread_string.c-lpthread
执行结果:
Theparameterpassedfrommainfunctioniszieckey pthreadiscreated...
例程总结:
可以看出来main函数中的字符串传入了新建的线程中。
例程4:
pthread_struct.c
zeickey **Date:
stdlib.h>
structmenber
inta;
char*s;
};
structmenber*temp;
temp=(structmenber*)arg;
menber->
a=%d\n"
temp->
a);
s=%s\n"
s);
intmain(intargc,char*argv[])
structmenber*b;
b=(structmenber*)malloc(sizeof(structmenber));
b->
a=4;
s="
tidp,NULL,create,(void*)b);
if(error)
phreadisnotcreated...\n"
pthreadiscreated...\n"
gcc-Wallpthread_struct.c-lpthread
menber->
a=4
s=zieckey
pthreadiscreated...
可以看出来main函数中的一个结构体传入了新建的线程中。
线程包含了标识进程内执行环境必须的信息。
他集成了进程中的所有信息都是对线程进行共享的,包括文本程序、程序的全局内存和堆内存、栈以及文件描述符。
例程5:
程序目的:
验证新建立的线程可以共享进程中的数据
pthread_share.c
pthread_share_data.c
**UsedtostudythemultithreadprogramminginLinuxOS
staticinta=4;
newpthread...\n"
a=%d\n"
a);
a=5;
tidp,NULL,create,NULL);
newthreadisnotcreate...\n"
newthreadiscreated...\n"
gcc-Wallpthread_share_data.c-lpthread
newpthread...
a=5
newthreadiscreated...
可以看出来,我们在主线程更改了我们的全局变量a的值的时候,我们新建立的线程则打印出来了改变的值,可以看出可以访问线程所在进程中的数据信息。
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
**Aexampleshowingathreadtoexitandwithareturncode.
return(void*)8;
intmain(intargc,char*argv[])
pthread_ttid;
void*temp;
tid,NULL,create,NULL);
threadisnotcreated...\n"
error=pthread_join(tid,&
temp);
threadisnotexit...\n"
return-2;
threadisexitcode%d\n"
(int)temp);
gcc-Wallpthread_exit.c-lpthread
执行结果:
newthreadiscreated...
threadisexitcode8
可以看出来,线程退出可以返回线程的int数值。
线程退出不仅仅可以返回线程的int数值,还可以返回一个复杂的数据结构。
例程7
线程结束返回一个复杂的数据结构
pthread_return_struct.c
char*b;
}
temp={8,"
};
void*create(void*arg)
{
printf("
newthread...\n"
return(void*)&
temp;
pthread_t