unix环境中线程的创建与终止
2015-01-29 22:01
211 查看
本篇来梳理一下apue第十一章中关于的三个知识点,因为线程的同步知识比较多,所以线程同步的知识在后面的文章中仔细梳理。
下面列出要梳理的三个知识点
1.线程标识
2.线程创建
3.线程终止
1线程标识
#include<pthread.h>
int pthread_equal(pthread_t t1,pthread_t ,t2);
比较线程id,如果相同则返回非零,否则返回零
man中有句话compile and link with -pthread表示编译时要链接静态函数库,因为pthread库不是linux默认的库,所以链接时要使用静态函数库libpthread.a,所以编译时要加-lpthread,在apue的程序清单11-1中会看到如果用gcc编译时如果没加-lpthread会报错11-1.c:(.text+0x83): undefined reference to `pthread_create'
2线程创建
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);//与apue中的不同,文章中的所有函数都是在ubuntu12.04中的man中查得
Pthread的四个参数:
(1)Thread指向的内存单元被设置为新创建的线程id,注意参数是指向的是pthread_t的指针,
(2)Attr用与指定thread所指向线程的属性
(3)Start_routine表示新创建的线程从start_routine函数的地址开始运行
(4)arg表示start_routine函数的参数,若要传递多个参数,则把多个参数放在arg结构体中。
需要注意的三点:
(1)线程创建是并不能保证那个线程先运行:是新创建的线程还是调用线程。
(2)新创建的线程可一访问进程的地址空间,比且继承调用线程的浮点环境和信号屏蔽字,但该进程的未决信号集被清除
(3)注意pthread函数在调用失败时通常会返回错误码,但并不像其他posix函数那样设置errno。
3线程终止
线程退出的3种方式
(1)直接从函数中返回即return
(2)被同一进程中的其它线程取消
(3)线程调用pthread_exit();
1.#include<pthread.h>
Void Pthread_exit(void *retval)
该函数用来终止线程,该函数的参数retval可以通过调用pthread_join函数来访问这个指针
2.#include<pthread.h>
int pthread_join(pthread_t thread, void **retval);
调用线程将一直被阻塞,直到指定的的线程调用pthread_exit,从函数中返回或者被取消
如果是从函数中返回retval包含返回码(即return的值),如果线程被取消,由retval指定的内存单元就被置为PTHREAD_CANCELED
如果对线程的返回值不感兴趣,可以retval的值置为NULL
3.#include<pthread.h>
int pthread_cancel(pthread_t thread);
(1)默认情况下,pthread_cancel函数会使得由thread标志的行为表现为如同调用了参数为PTHREAD_CANCELED的pthread_exit的函数
(2)线程可以选择忽略取消方式或控制取消方式
(3)Pthread_cancel并不等待线程终止,它仅仅提出请求。
4.#include<pthread.h>
Void pthread_cleanup_push(void (*rtn) (void*),void *arg);
Void pthread_cleanup_pop(int execute);
当线程在执行以下动作时调用清理函数,调用参数为arg,调用顺序为在pthread_clean_push函数中先注册的后调用:
(1)调用pthread_exit函数时
(2)调用pthread_cancel并响应取消请求时
(3)用非零execute参数调用pthread_cleanup_pop时
需要注意的是这两个函数都是用宏来实现的
#define pthread_cleanup_push(routine,arg) {struct _pthread_cleanup_buffer; _pthread_cleanup_push(&_buffer,(routine)(arg));
#define pthread_cleanup_pop(execute) _pthread_cleanup_pop(&_buffer,(execute));}
所以这两个函数必须成对出现。
下面列出要梳理的三个知识点
1.线程标识
2.线程创建
3.线程终止
1线程标识
#include<pthread.h>
int pthread_equal(pthread_t t1,pthread_t ,t2);
比较线程id,如果相同则返回非零,否则返回零
man中有句话compile and link with -pthread表示编译时要链接静态函数库,因为pthread库不是linux默认的库,所以链接时要使用静态函数库libpthread.a,所以编译时要加-lpthread,在apue的程序清单11-1中会看到如果用gcc编译时如果没加-lpthread会报错11-1.c:(.text+0x83): undefined reference to `pthread_create'
2线程创建
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);//与apue中的不同,文章中的所有函数都是在ubuntu12.04中的man中查得
Pthread的四个参数:
(1)Thread指向的内存单元被设置为新创建的线程id,注意参数是指向的是pthread_t的指针,
(2)Attr用与指定thread所指向线程的属性
(3)Start_routine表示新创建的线程从start_routine函数的地址开始运行
(4)arg表示start_routine函数的参数,若要传递多个参数,则把多个参数放在arg结构体中。
需要注意的三点:
(1)线程创建是并不能保证那个线程先运行:是新创建的线程还是调用线程。
(2)新创建的线程可一访问进程的地址空间,比且继承调用线程的浮点环境和信号屏蔽字,但该进程的未决信号集被清除
(3)注意pthread函数在调用失败时通常会返回错误码,但并不像其他posix函数那样设置errno。
3线程终止
线程退出的3种方式
(1)直接从函数中返回即return
(2)被同一进程中的其它线程取消
(3)线程调用pthread_exit();
1.#include<pthread.h>
Void Pthread_exit(void *retval)
该函数用来终止线程,该函数的参数retval可以通过调用pthread_join函数来访问这个指针
2.#include<pthread.h>
int pthread_join(pthread_t thread, void **retval);
调用线程将一直被阻塞,直到指定的的线程调用pthread_exit,从函数中返回或者被取消
如果是从函数中返回retval包含返回码(即return的值),如果线程被取消,由retval指定的内存单元就被置为PTHREAD_CANCELED
如果对线程的返回值不感兴趣,可以retval的值置为NULL
3.#include<pthread.h>
int pthread_cancel(pthread_t thread);
(1)默认情况下,pthread_cancel函数会使得由thread标志的行为表现为如同调用了参数为PTHREAD_CANCELED的pthread_exit的函数
(2)线程可以选择忽略取消方式或控制取消方式
(3)Pthread_cancel并不等待线程终止,它仅仅提出请求。
4.#include<pthread.h>
Void pthread_cleanup_push(void (*rtn) (void*),void *arg);
Void pthread_cleanup_pop(int execute);
当线程在执行以下动作时调用清理函数,调用参数为arg,调用顺序为在pthread_clean_push函数中先注册的后调用:
(1)调用pthread_exit函数时
(2)调用pthread_cancel并响应取消请求时
(3)用非零execute参数调用pthread_cleanup_pop时
需要注意的是这两个函数都是用宏来实现的
#define pthread_cleanup_push(routine,arg) {struct _pthread_cleanup_buffer; _pthread_cleanup_push(&_buffer,(routine)(arg));
#define pthread_cleanup_pop(execute) _pthread_cleanup_pop(&_buffer,(execute));}
所以这两个函数必须成对出现。
相关文章推荐
- UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
- UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
- Unix 环境高级编程---线程创建、同步、
- unix 环境高级编程 线程一 创建
- UNIX环境高级编程之创建进程与线程
- UNIX环境高级编程11.5线程终止
- UNIX环境高级编程学习之第十一章线程-线程的创建、退出、等待、取消、分离
- UNIX环境高级编程学习之第十二章线程控制-以分离状态创建线程
- 如何:创建和终止线程(C# 编程指南--MSDN)
- 为学习APUE(Unix环境高级编程)偷懒,而写的脚本,基本上相当于一个简单的工程创建脚本了
- asp.net 创建和终止线程(多线程)
- 如何:创建和终止线程(C# 编程指南)
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 在内核模式下创建和终止线程
- 如何:创建和终止线程
- 多线程编程-线程的创建和终止
- UNIX环境高级编程学习之第九章进程关系-创建孤儿进程
- 如何创建和终止线程(转载自MSDN)
- UNIX环境高级编程学习之第十二章线程控制-可重入(线程安全)的getenv方法