- 浏览: 223283 次
- 性别:
- 来自: 北京
最新评论
-
qq452739204:
如果构造的报文大于mtu值,是否需要分片之后再发送出去列?
Linux内核构造数据包并发送(二)(dev_queue_xmit方式) -
xingzengmou:
你好,我搞的原理跟你差不多,但播放的时候有papapa的声音, ...
AudioRecord和AudioTrack类的使用 -
wenjiefeng:
楼主,你有录制pcm格式和播放pcm格式的录音器的demo吗, ...
AudioRecord和AudioTrack类的使用 -
lovepeakingA:
...
AudioRecord和AudioTrack类的使用 -
ZaneLee007:
假的,不学无术
Android禁用键盘的所有按键
线程的创建是用下面的几个函数来实现的.
#include <pthread.h> 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 <stdio.h>
#include <unistd.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <pthread.h> #include <dirent.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #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:%s\n\a",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!\n\a"); exit(1); } for(i=0,j=0;i<num;i++) { memset(filename,'\0',BUFFER); strcpy(filename,namelist[i]->d_name); if(stat(filename,&filestat)==-1) { fprintf(stderr,"Get File Information:%s\n\a",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:%s\n\a",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:%s\n\a",filename,strerror(errno )); continue; } /* 创建线程,进行文件拷贝 */ if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0) fprintf(stderr,"Create Thread[%d] Error:%s\n\a",i,strerror(errno)); j++; } byte_copy=0; for(i=0;i<j;i++) { /* 等待线程结束 */ if(pthread_join(thread[i],(void **)&byte_copy_p)!=0) fprintf(stderr,"Thread[%d] Join Error:%s\n\a", i,strerror(errno)); else { if(bytes_copy_p==NULL)continue; printf("Thread[%d] Copy %d bytes\n\a",i,*byte_copy_p); byte_copy+=*byte_copy_p; /* 释放我们在copy函数里面创建的内存 */ free(byte_copy_p); } } printf("Total Copy Bytes %d\n\a",byte_copy); free(thread); free(file); exit(0); }
发表评论
-
ARP头
2010-12-01 17:20 1185ifndef _NET_IF_ARP_H_ #d ... -
自己用libpcap来抓包
2010-12-01 16:40 6921#define APP_NAME &quo ... -
MAC文本格式地址to网络字节序
2010-12-01 16:38 2096#include <sys/types.h& ... -
C语言实现修改IE浏览器的代理设置
2010-11-24 17:18 4574写了一个用C语言实现修 ... -
[转]通过例子学习Lua
2010-11-17 21:25 4663据说本文作者是OGDEV的HACK达人 通过例子学习 ... -
size_t和ssize_t类型
2010-10-22 10:11 1695size_t 是为了方便系统之间的移植而定义的在32位系统 ... -
高速网络环境下基于零拷贝的报文捕获机制
2010-10-19 09:07 4697参看: http://www.linuxjo ... -
sendfile() -- 通过 socket 拷贝文件
2010-10-19 08:53 2653原型: #include <sys/sendfi ... -
零拷贝与 sendfile
2010-10-19 08:47 3671最近在搞zerocopy的东西,看到了这篇文章,拷过来备用! ... -
C++队列实现和队列简介
2010-10-09 11:04 2807队列简介: 队列(Queue)是一种数据结构,可以在队 ... -
定长队列写入文件并读取
2010-10-09 10:57 1344#include "stdio.h" ... -
循环队列
2010-10-09 10:55 1641相信绝大多数都学习过《数据结构》这门课程,而对这门课程里 ... -
自动、静态、 寄存器、易失变量的区别
2010-09-30 08:08 1557自动变量 什么是 ... -
stdarg.h
2010-09-27 09:12 1779stdarg.h是C语言中C标 ... -
extern用法详解
2010-09-27 08:33 13481 基本解释 extern ... -
Linux Daemon In C
2010-09-26 08:22 2332This is a template for a Lin ... -
typedef 的用法
2010-09-25 20:43 895用途一: 定义一种类型的别名,而不只是简单 ... -
编写守护进程
2010-09-17 10:43 864#include <stdio.h> #incl ... -
抓包程序的包存储队列
2010-09-17 09:08 775typedef struct _Node { in ... -
浅谈C中的malloc和free
2010-09-03 19:37 871在C语言的学习中,对内存管理这部分的知识掌握尤其重要!之前对C ...
相关推荐
pthread_t *tids;//线程id unsigned waiting_tasks;//等待任务 unsigned active_threads;// bool shutdown;//停始状态 }thread_pool; //初始化线程池 bool init_pool(thread_pool *pool, unsigned int ...
注意:不应使用全局变量 pthread_t tid,在子线程中通过pthread_create传出参数来获取线程ID,而应使用pthread_self。 pthread_create函数 创建一个新线程。 其作用,对应进程中fork() 函数。 int pthread_create...
线程属性 线程具有属性,⽤ pthread_attr_t 表⽰ pthread_attr_t attr; //初始化 attr中为操作系统实现⽀持的线程所有属性的默认值 pthread_attr_init(&attr); pthread_attr_destroy(&attr); 分离线程 线程创建默认...
Linux线程同步之间存在多种机制,条件变量是一种类似操作系统里提到的生产者-消费者算法的同步机制,允许线程以无竞争的方式等待特定条件的发生。 示例伪代码: void* Thread1(void){ while(线程运行条件成立){ ...
pthread_piano-DE1-SOC 在钢琴上将中级C范围合成为多线程并与音频CODEC练习一起使用。 该程序有两个线程,一个线程侦听键盘输入以模拟钢琴键,另一个线程从按下的键计算样本。 tone_volume是互斥变量,并使用衰减...
__thread是GCC内置的线程局部存储设施(Thread-Local Storage),它的实现非常高效,与pthread_key_t向比较更为快速,其存储性能可以与全局变量相媲美,而且使用方式也更为简单。创建线程局部变量只需简单的在...
http://blog.csdn.net/infoworld/article/details/49715355 项目代码,mingw编译.
作者:曹忠明,华清远见嵌入式学院讲师。 volatile在词典中的意思是易变的,反复无常的。它在我们的程序设计中常常用到的。volatile是一个关键字... pthread_t my_thread; int err,k; if ((err = pthread_cr
DWORD WINAPI ThreadFun(LPVOID pthread) //线程入口函数 { lpthread temp=(lpthread)pthread;//参数强制转换为结构体类型 temp->progress->SetPos(temp->pos); //设置被传递过来的进度条的位置 while(temp->pos)...
connfd = accept(listenfd, (struct sockaddr *)&cliaddr,(socklen_t *)&size ); //子进程accept请求 rep.status = 'n'; if (write(fd1[1], &rep, sizeof(rep)) ) { //通知父进程已经accept了请求 perror("write...
Posix-Thread-UI 简单的示例代码演示了几个 Pthread 条件变量例程的使用。 主例程创建三个线程。 其中两个线程执行工作并更新“计数”变量。 第三个线程等待,直到计数变量达到指定值。 参考: :
是动态分配,要释放 3)关于改进: 1. 回收资源的线程和TaskList reclaim[LEVEL_ARRAY_LIMIT];是可以省掉的 但考虑到模块功能的延伸: 如果任务执行失败,先移动到某个地方,过段时间在放回来重试,等等... 其实...
2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1 3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2 4) 线程序1需要在线程2退出后才能退出 5) 主线程在检测到g_Flag从1...
多线程基础介绍.........................................................................................................................................15 定义多线程术语...................................
__thread的协程私有变量、协程间通信的协程信号量co_signal (New); 语言级别的lambda实现,结合协程原地编写并执行后台异步任务 (New); 基于epoll/kqueue实现的小而轻的网络框架,基于时间轮盘实现的高性能定时器;
1 多线程基础介绍15 定义多线程术语15 符合多线程标准16 多线程的益处17 提高应用程序的响应 17 有效使用多处理器17 改进程序结构17 占用较少的系统资源17 结合线程和RPC(远程过程调用)18 多线程概念18 并发性和...
7.3.2 用pthread_create创建线程 168 7.3.3 更新示例应用程序以使用POSIX线程 169 7.3.4 执行POSIX线程示例 174 7.4 从POSIX线程返回结果 174 7.5 POSIX线程同步 176 7.5.1 用互斥锁同步POSIX线程 176 7.5.2 ...
一、子线程无法访问父线程的全局变量,但父线程可以访问子线程的变量 <?php class Task extends Thread { public $data; public function run() { global $num; var_dump($num); $this->data = 'abc'; //...
比如2个线程对变量进行加1操作,第1个线程还没来的及改写数据时,第2个线程就对变量进行操作了,那变量最终的结果就是未知的,这个时候就需要同步来进行控制了。 例子如下: <?php class Count extends Thread {...