Linux系统下C语言编程:线程的创建和使用
2014-05-14 09:33
681 查看
Linux系统下C语言编程:线程的创建和使用 -
线程的创建是用下面的几个函数来实现的.
#include int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start_routine)(void *),void *arg);void pthread_exit(void *retval);int pthread_join(pthread *thread,void **thread_return);
pthread_create创建一个线程,thread是用来表明创建线程的ID,attr指出线程创建时候的属性,我们用NULL来表明使用缺省属性。start_routine函数指针是线程创建成功后开始执行的函数,arg是这个函数的唯一一个参数。表明传递给start_routine的参数。
pthread_exit函数和exit函数类似用来退出线程.这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用pthread_join函数等待它。然后将*retval的值传递给**thread_return.由于这个函数释放所以的函数资源,所以retval不能够指向函数的局部变量。
pthread_join和wait调用一样用来等待指定的线程。下面我们使用一个实例来解释一下使用方法.在实践中,我们经常要备份一些文件。下面这个程序可以实现当前目录下的所有文件备份.备份后的后缀名为bak
#include #include #include #include #include #include #include #include #include #include #include #define BUFFER 512 struct copy_file { int infile; int outfile; }; void *copy(void *arg) { int infile,outfile; int bytes_read,bytes_write,*bytes_copy_p;
char buffer[BUFFER],*buffer_p; struct copy_file *file=(struct copy_file *)arg; infile=file->infile; outfile=file->outfile; /* 因为线程退出时,所有的变量空间都要被释放,所以我们只好自己分配内存了 */ if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL); bytes_read=bytes_write=0;
*bytes_copy_p=0; while((bytes_read=read(infile,buffer,BUFFER))!=0) { if((bytes_read==-1)(errno!=EINTR))break; else if(bytes_read>0) { buffer_p=buffer; while((bytes_write=write(outfile,buffer_p,bytes_read))!=0) { if((bytes_write==-1)(errno!=EINTR))break; else
if(bytes_write==bytes_read)break; else if(bytes_write>0) { buffer_p =bytes_write; bytes_read-=bytes_write; } } if(bytes_write==-1)break; *bytes_copy_p =bytes_read; } } close(infile); close(outfile); pthread_exit(bytes_copy_p); } int main(int argc,char **argv)
{ pthread_t *thread; struct copy_file *file; int byte_copy,*byte_copy_p,num,i,j; char filename[BUFFER]; struct dirent **namelist; struct stat filestat; /* 得到当前路径下面所有的文件(包含目录)的个数 */ if((num=scandir(".",namelist,0,alphasort))<0) { fprintf(stderr,"Get File Num
Error:%sna",strerror(errno)); exit(1); } /* 给线程分配空间,其实没有必要这么多的 */ if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)|| ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL) ) { fprintf(stderr,"Out Of Memory!na"); exit(1); } for(i=0,j=0;id_name);
if(stat(filename,filestat)==-1) { fprintf(stderr,"Get File Information:%sna",strerror(errno)); exit(1); } /* 我们忽略目录 */ if(!S_ISREG(filestat.st_mode))continue; if((file[j].infile=open(filename,O_RDONLY))<0) { fprintf(stderr,"Open %s Error:%sna",filename,strerror(errno));
continue; } strcat(filename,".bak"); if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) <0) { fprintf(stderr,"Creat %s Error:%sna",filename,strerror(errno )); continue; } /* 创建线程,进行文件拷贝 */ if(pthread_create(thread[j],NULL,copy,(void *)file[j])!=0)
fprintf(stderr,"Create Thread[%d] Error:%sna",i,strerror(errno)); j ; } byte_copy=0; for(i=0;i
线程的创建是用下面的几个函数来实现的.
#include int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start_routine)(void *),void *arg);void pthread_exit(void *retval);int pthread_join(pthread *thread,void **thread_return);
pthread_create创建一个线程,thread是用来表明创建线程的ID,attr指出线程创建时候的属性,我们用NULL来表明使用缺省属性。start_routine函数指针是线程创建成功后开始执行的函数,arg是这个函数的唯一一个参数。表明传递给start_routine的参数。
pthread_exit函数和exit函数类似用来退出线程.这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用pthread_join函数等待它。然后将*retval的值传递给**thread_return.由于这个函数释放所以的函数资源,所以retval不能够指向函数的局部变量。
pthread_join和wait调用一样用来等待指定的线程。下面我们使用一个实例来解释一下使用方法.在实践中,我们经常要备份一些文件。下面这个程序可以实现当前目录下的所有文件备份.备份后的后缀名为bak
#include #include #include #include #include #include #include #include #include #include #include #define BUFFER 512 struct copy_file { int infile; int outfile; }; void *copy(void *arg) { int infile,outfile; int bytes_read,bytes_write,*bytes_copy_p;
char buffer[BUFFER],*buffer_p; struct copy_file *file=(struct copy_file *)arg; infile=file->infile; outfile=file->outfile; /* 因为线程退出时,所有的变量空间都要被释放,所以我们只好自己分配内存了 */ if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL); bytes_read=bytes_write=0;
*bytes_copy_p=0; while((bytes_read=read(infile,buffer,BUFFER))!=0) { if((bytes_read==-1)(errno!=EINTR))break; else if(bytes_read>0) { buffer_p=buffer; while((bytes_write=write(outfile,buffer_p,bytes_read))!=0) { if((bytes_write==-1)(errno!=EINTR))break; else
if(bytes_write==bytes_read)break; else if(bytes_write>0) { buffer_p =bytes_write; bytes_read-=bytes_write; } } if(bytes_write==-1)break; *bytes_copy_p =bytes_read; } } close(infile); close(outfile); pthread_exit(bytes_copy_p); } int main(int argc,char **argv)
{ pthread_t *thread; struct copy_file *file; int byte_copy,*byte_copy_p,num,i,j; char filename[BUFFER]; struct dirent **namelist; struct stat filestat; /* 得到当前路径下面所有的文件(包含目录)的个数 */ if((num=scandir(".",namelist,0,alphasort))<0) { fprintf(stderr,"Get File Num
Error:%sna",strerror(errno)); exit(1); } /* 给线程分配空间,其实没有必要这么多的 */ if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)|| ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL) ) { fprintf(stderr,"Out Of Memory!na"); exit(1); } for(i=0,j=0;id_name);
if(stat(filename,filestat)==-1) { fprintf(stderr,"Get File Information:%sna",strerror(errno)); exit(1); } /* 我们忽略目录 */ if(!S_ISREG(filestat.st_mode))continue; if((file[j].infile=open(filename,O_RDONLY))<0) { fprintf(stderr,"Open %s Error:%sna",filename,strerror(errno));
continue; } strcat(filename,".bak"); if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) <0) { fprintf(stderr,"Creat %s Error:%sna",filename,strerror(errno )); continue; } /* 创建线程,进行文件拷贝 */ if(pthread_create(thread[j],NULL,copy,(void *)file[j])!=0)
fprintf(stderr,"Create Thread[%d] Error:%sna",i,strerror(errno)); j ; } byte_copy=0; for(i=0;i
相关文章推荐
- Linux系统下C语言编程:线程的创建和使用
- C# 多线程编程 - 使用Thread类创建线程
- 多进程并发编程----基于高级的预先创建进程池(accept使用线程上锁)的模型
- java多线程编程之使用thread类创建线程
- java并发编程---如何创建线程以及Thread类的使用
- 易语言中简化了类的创建与使用,使得面对对象编程更简单
- [C++11 并发编程] 12 使用条件变量创建线程间安全的队列
- Linux系统编程:fork函数的使用【循环创建N个子线程】
- java多线程编程之使用runnable接口创建线程
- C# 多线程编程 - 使用委托创建线程
- Linux系统下C语言编程 线程操作入门常识
- 使用ADO.NET和C#以编程方式创建 SQL Server 数据库
- 在ASP.NET AJAX中使用应用程序服务和本地化(6):创建支持多语言的ASP.NET AJAX站点、小结
- Linux下C语言编程的-编译命令gcc的使用
- Linux下C语言编程--进程的创建
- 编程使用资源文件实现多语言页面(In Action)
- VS2005 C#创建WebService使用线程
- 使用 OpenSSL API 进行安全编程--创建基本的安全连接和非安全连接
- Posix线程编程指南(1)---线程创建与取消
- 游戏编程——我该使用何种语言