linux 多线程技术

上传人:第*** 文档编号:54571039 上传时间:2018-09-15 格式:PPT 页数:25 大小:1.60MB
返回 下载 相关 举报
linux 多线程技术_第1页
第1页 / 共25页
linux 多线程技术_第2页
第2页 / 共25页
linux 多线程技术_第3页
第3页 / 共25页
linux 多线程技术_第4页
第4页 / 共25页
linux 多线程技术_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《linux 多线程技术》由会员分享,可在线阅读,更多相关《linux 多线程技术(25页珍藏版)》请在金锄头文库上搜索。

1、Linux 多线程技术,POSIX 线程库Pthreads,使用fork() 创建进程 代价昂贵 进程间通信方式较复杂 操作系统在实现进程间的切换比线程切换更费时,使用pthreads库创建线程 创建进程比创建线程更快 线程间的通信方式更容 操作系统对线程的切换比对进程的切换更容易和快速,线程的创建,#include int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg ); 第一个参数为指向线程标识符的指针。 第二个参数用来设置线程属性。

2、 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数。 当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。,一个简单例子,#include #include #include #include #include pthread_t ntid; void *thr_fn(void *arg) printids(“new thread:“); return (void *)0); int main() int err; err = pth

3、read_create( ,编译多线程程序,gcc -o mypthread -lpthread mypthread.c,线程的退出,调用pthread_exit()结束线程执行 void pthread_exit(void *retval); 让线程处理程序返回 使用 pthread_cancel() 函数终止其他线程的执行 int pthread_cancel(pthread_t thread); 向线程t发送取消请求,默认情况下线程thread自己调用pthread_exit(PTHREAD_CANCELED),,等待线程结束,使用 pthread_join() 函数等待被创建的线程结束

4、 pthread_join() 函数会挂起创建线程的线程的执行 直到等待到想要等待的子线程 函数原型 : int pthread_join(pthread_t th, void *thread_return);,线程的分离,主线程可以不断地创建子线程 子线程本身自己有自我回收内存资源的能力 函数原型: int pthread_detach(pthread_t th); pthread_detach() 和 pthread_join() 一般情况下不能同时使用,获得当前线程的标志,pthread_t pthread_self(void); 本函数返回本线程的标识符。 在LinuxThreads中

5、,每个线程都用一个pthread_descr结构来描述,其中包含了线程状态、线程ID等所有需要的数据结构,此函数的实现就是在线程栈帧中找到本线程的pthread_descr结构,然后返回其中的p_tid项。,一个例子,#include #include #include #include #define THREAD_NUMBER 2 int retval_hello1= 2, retval_hello2 = 3; void* hello1(void *arg) char *hello_str = (char *)arg; sleep(1); printf(“%sn“, hello_str);

6、 pthread_exit( ,void* hello2(void *arg) char *hello_str = (char *)arg; sleep(2); printf(“%sn“, hello_str); pthread_exit(,printf(“Begin to create threads.n“); ret_val = pthread_create( ,printf(“Begin to wait for threads.n“); for(i = 0; i THREAD_NUMBER; i+) ret_val = pthread_join(pti, (void *) ,线程属性的初

7、始化和撤销,线程初始化: int pthread_attr_init(pthread_attr_t *attr) 初始化线程属性对象attr,并用默认值填充 线程撤销: int pthread_attr_destroy(pthread_attr_t *attr) 销毁线程属性对象attr。 *修改线程属性对象attr只有在线程创建前有效,在线程创建后修改对当前线程不起作用。 *返回0成功,否则失败。,pthread_attr_t定义,pthread_attr_t定义: typedef struct _pthread_attr_s int _detachstate; int _schedpoli

8、cy; struct _sched_param _schedparam; int _inheritsched; int _scope; size_t _guardsize; int _stackaddr_set; void *_stackaddr; size_t _stacksize; pthread_attr_t;,线程的属性,线程的属性(续),相关函数-分离状态,设置分离状态:pthread_attr_setdetachstate int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 返回值:函数成功

9、返回0;任何其他返回值都表示错误 设置分离状态。 参数detachstate的值为:PTHREAD_CREATE_DETACHED、PTHREAD_CREATE_JOINABLE。 获取分离状态:pthread_attr_getdetachstate int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate); 返回值:函数成功返回0;任何其他返回值都表示错误 取线程分离状态:分离的或是非分离的。,相关函数-调度策略,设置调度策略:pthread_attr_setschedpolicy int pthread_

10、attr_setschedpolicy(pthread_attr_t *tattr, int policy); 返回值:函数成功返回0;任何其他返回值都表示错误 。 POSIX标准定义的调度策略有:SCHED_FIFO(先入先出)、SCHED_RR(循环)、SCHED_OTHER(由不同版本的POSIX线程库定义的缺省调度策略)。 获取调度策略:pthread_attr_getschedpolicy int pthread_attr_getschedpolicy(pthread_attr_t *tattr, int *policy); 返回值:函数成功返回0;任何其他返回值都表示错误 。,相关

11、函数-调度参数,设置调度参数:pthread_attr_setschedparam int pthread_attr_setschedparam(pthread_attr_t *tattr, const struct sched_param *param); 返回值:函数成功返回0;任何其他返回值都表示错误 。 属性对象的调度参数定义在param结构中;在这个结构中只定义了优先级priority成员。新创建线程的优先级由属性对象中param结构的priority参数指定。 有两种方式可以修改线程的优先级。可以在创建子线程前设置属性对象的优先级参数;也可以先修改父线程的优先级,然后创建子线程。

12、sched_param结构中有可能存放着其他一些调度信息。所以在修改线程属性对象的调度参数前先取现有的调度参数是良好的习惯。 一段合理的代码应该是这样的:先取线程属性对象中现有的调度参数,对取出的调度参数进行操作,再用修改过的调度参数重置线程属性对象。 获取调度参数:pthread_attr_getschedparam int pthread_attr_getschedparam(pthread_attr_t *tattr, const struct sched_param *param); 返回值:函数成功返回0;任何其他返回值都表示错误 。,相关函数-域,设置域:pthread_attr_

13、setscope int pthread_attr_setscope(pthread_attr_t *tattr, int scope); 返回值:函数成功返回0;任何其他返回值都表示错误 。 指定将来创建的线程是绑定(PTHREAD_SCOPE_SYSTEM)的还是非绑定的(PTHREAD_SCOPE_PROCESS)。 在一个进程中可以同时有这两种不同类型的线程。 获取域:pthread_attr_getscope int pthread_attr_getscope(pthread_attr_t *tattr, int *scope); 返回值:函数成功返回0;任何其他返回值都表示错误。,

14、例子:threadattr,#include #include #include void* sum_val(void *arg) int sum = 0; int i; int count = *(int *)arg; for (i = 0; i count; i+) sum = sum + i; printf(“sum is %dn“, sum); pthread_exit(0); ,例子:threadattr(续),int main(int argc, char *argv) pthread_t pt; int count = 10; int ret_val; pthread_attr_t attr; struct sched_param sp; sp._sched_priority = 2; ret_val = pthread_attr_init( ,例子:threadattr(续),ret_val = pthread_attr_setdetachstate( ,例子:threadattr(续),ret_val = pthread_attr_setinheritsched( ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号