【Linux C编程】第十四章 线程、线程控制、线程属性

举报
Yuchuan 发表于 2021/05/28 19:23:28 2021/05/28
【摘要】   应避免在多线程模型中调用fork除非,马上exec,子进程中只有调用fork的线程存在,其他线程在子进程中均pthread_exit。   信号的复杂语义很难和多线程共存,应避免在多线程引入信号机制。

一、整体大纲

二、线程相关

1. 什么是线程     

    LWP:light weight process 轻量级的进程,本质仍是进程(在Linux环境下)

    进程:独立地址空间,拥有PCB

    线程:也有PCB,但没有独立的地址空间(共享)

    区别:在于是否共享地址空间。 独居(进程);合租(线程)。

    Linux下: 线程:最小的执行单位

   进程:最小分配资源单位,可看成是只有一个线程的进程。

2. Linux内核线程实现原理

    (1)线程实现原理

    类Unix系统中,早期是没有“线程”概念的,80年代才引入,借助进程机制实现出了线程的概念。因此在这类系统中,进程和线程关系密切。

    1)轻量级进程(light-weight process),也有PCB,创建线程使用的底层函数和进程一样,都是clone

    2)从内核里看进程和线程是一样的,都有各自不同的PCB,但是PCB中指向内存资源的三级页表是相同的

    3)进程可以蜕变成线程

    4)线程可看做寄存器和栈的集合

    5)在linux下,线程最是小的执行单位;进程是最小的分配资源单位

    察看LWP号:ps -Lf pid 查看指定线程的lwp号。

    三级映射:进程PCB --> 页目录(可看成数组,首地址位于PCB中) --> 页表 --> 物理页面 --> 内存单元

    参考:《Linux内核源代码情景分析》 ----毛德操

    对于进程来说,相同的地址(同一个虚拟地址)在不同的进程中,反复使用而不冲突。原因是他们虽虚拟址一样,但,页目录、页表、物理页面各不相同。相同的虚拟址,映射到不同的物理页面内存单元,最终访问不同的物理页面。

    但!线程不同!两个线程具有各自独立的PCB,但共享同一个页目录,也就共享同一个页表和物理页面。所以两个PCB共享一个地址空间。

    实际上,无论是创建进程的fork,还是创建线程的pthread_create,底层实现都是调用同一个内核函数clone。

    如果复制对方的地址空间,那么就产出一个“进程”;如果共享对方的地址空间,就产生一个“线程”。

    因此:Linux内核是不区分进程和线程的。只在用户层面上进行区分。所以,线程所有操作函数 pthread_* 是库函数,而非系统调用。

    (2)线程共享资源

     1)文件描述符表

     2)每种信号的处理方式

     3)当前工作目录

     4)用户ID和组ID

     5)内存地址空间 (.text/.data/.bss/heap/共享库)

    (3)线程非共享资源

     1)线程id

     2)处理器现场和栈指针(内核栈)

     3)独立的栈空间(用户空间栈)

     4)errno变量

     5)信号屏蔽字

     6)调度优先级

   (4)线程优、缺点

    优点:

          1)提高程序并发性

          2)开销小

          3)数据通信、共享数据方便

    缺点:

          1)库函数,不稳定

          2)调试、编写困难、gdb不支持

          3)对信号支持不好

    优点相对突出,缺点均不是硬伤。Linux下由于实现方法导致进程、线程差别不是很大。

3. 线程控制相关函数

    (1)pthread_self函数

     获取线程ID。其作用对应进程中 getpid() 函数。

pthread_t pthread_self(void); 返回值:成功:0; 失败:无

    线程ID:pthread_t类型,本质:在Linux下为无符号整数(%lu),其他系统中可能是结构体实现

    线程ID是进程内部,识别标志。(两个进程间,线程ID允许相同)

    注意:不应使用全局变量 pthread_t tid,在子线程中通过pthread_create传出参数来获取线程ID,而应使用pthread_self。

    (2)pthread_create函数

    创建一个新线程。 其作用,对应进程中fork() 函数。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

    返回值:成功:0; 失败:错误号 -----Linux环境下,所有线程特点,失败均直接返回错误号。

    参数:

           thread 线程的id,传出参数,pthread_t:当前Linux中可理解为:typedef  unsigned long int  pthread_t;

           attr 代表线程的属性,通常传NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。

           第三个参数函数指针 void *func(void *),指向线程主函数(线程体),该函数运行结束,则线程结束。

           arg 线程主函数执行期间所使用的参数。

    返回值:

          成功返回0

          失败返回errno

    注意:编译的时候需要加pthread库(Compile and link with -pthread.)

    在一个线程中调用pthread_create()创建新的线程后,当前线程从pthread_create()返回继续往下执行,而新的线程所执行的代码由我们传给pthread_create的函数指针start_routine决定。

start_routine函数接收一个参数,是通过pthread_create的arg参数传递给它的,该参数的类型为void *,这个指针按什么类型解释由调用者自己定义。start_routine的返回值类型也是void *,这个指针

的含义同样由调用者自己定义。start_routine返回时,这个线程就退出了,其它线程可以调用pthread_join得到start_routine的返回值,类似于父进程调用wait(2)得到子进程的退出状态,稍后详细介绍pthread_join。

    pthread_create成功返回后,新创建的线程的id被填写到thread参数所指向的内存单元。我们知道进程id的类型是pid_t,每个进程的id在整个系统中是唯一的,调用getpid(2)可以获得当前进程的

id,是一个正整数值。线程id的类型是thread_t,它只在当前进程中保证是唯一的,在不同的系统中thread_t这个类型有不同的实现,它可能是一个整数值,也可能是一个结构体,也可能是一个地

址,所以不能简单地当成整数用printf打印,调用pthread_self(3)可以获得当前线程的id。

    attr参数表示线程属性,本节不深入讨论线程属性,所有代码例子都传NULL给attr参数,表示线程属性取缺省值,感兴趣的读者可以参考APUE。

练习:创建一个新线程,打印线程ID。注意:链接线程库 -lpthread 

pthread_create示例

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 
 5 void *thr(void *arg)
 6 {
 7     printf("I am a thread! pid = %d, tid = %lu\n", getpid(), pthread_self());
 8     return NULL;
 9 }
10 
11 int main()
12 {
13     pthread_t tid;
14     pthread_create(&tid, NULL, thr, NULL);
15     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
16     sleep(1);
17 
18     return 0;
19 }

    由于pthread_create的错误码不保存在errno中,因此不能直接用perror(3)打印错误信息,可以先用strerror(3)把错误码转换成错误信息再打印。如果任意一个线程调用了exit或_exit,则整个进程的所有线程都终止,由于从main函数return也相当于调用exit,为了防止新创建的线程还没有得到执行就终止,我们在main函数return之前延时1秒,这只是一种权宜之计,即使主线程等待1秒,内核也不一定会调度新创建的线程执行,下一节我们会看到更好的办法。

练习:循环创建多个线程,每个线程打印自己是第几个被创建的线程。(类似于进程循环创建子进程) 

n_pthread_create.c

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 void *thr(void *arg)
 8 {
 9     int num = (int)arg;
10     printf("I am %d thread, self = %lu\n", num, pthread_self());
11 
12     return (void *)(100 + num);
13 }
14 
15 int main()
16 {
17     pthread_t tid[5];
18     int i = 0;
19     for (i = 0; i < 5; i++)
20     {
21         pthread_create(&tid[i], NULL, thr, (void*)i);
22     }
23 
24     for (i = 0; i < 5; i++)
25     {
26         void *ret;
27         pthread_join(tid[i], &ret);
28         printf("i = %d, ret = %d\n", i, (int)ret);
29     }
30 
31     return 0;
32 }

拓展思考:将pthread_create函数参4修改为(void *)&i, 将线程主函数内改为 i=*((int *)arg) 是否可以?

    线程与共享

    线程间共享全局变量!

    注意:线程默认共享数据段、代码段等地址空间,常用的是全局变量。而进程不共享全局变量,只能借助mmap。

    练习:设计程序,验证线程之间共享全局数据。

线程共享全局变量

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 int var = 100;
 8 
 9 void *thr(void *arg)
10 {
11     printf("I am a thread, self = %lui, var = %d\n", pthread_self(), var);
12     sleep(2);
13     var = 101;
14     printf("I am a thread, self = %lu, var = %d\n", pthread_self(), var);
15 
16     return NULL;
17 }
18 
19 int main()
20 {
21     pthread_t tid;
22     pthread_create(&tid, NULL, thr, NULL);
23     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
24 
25     pthread_detach(tid); //线程分离
26     printf("I am main thread, self = %lui, var = %d\n", pthread_self(), var);
27     var = 1003;
28     sleep(5);
29     printf("I am main thread, self = %lui, var = %d\n", pthread_self(), var);
30 
31     return 0;
32 }

    (3)pthread_exit函数

    将单个线程退出

void pthread_exit(void *retval); 参数:retval表示线程退出状态,通常传NULL

    思考:使用exit将指定线程退出,可以吗? 

    结论:线程中,禁止使用exit函数,会导致进程内所有线程全部退出。

    在不添加sleep控制输出顺序的情况下。pthread_create在循环中,几乎瞬间创建5个线程,但只有第1个线程有机会输出(或者第2个也有,也可能没有,取决于内核调度)如果第3个线程执行了

exit,将整个进程退出了,所以全部线程退出了。

    所以,多线程环境中,应尽量少用,或者不使用exit函数,取而代之使用pthread_exit函数,将单个线程退出。任何线程里exit导致进程退出,其他线程未工作结束,主控线程退出时不能return或exit。

    另注意,pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

    练习:编写多线程程序,总结exit、return、pthread_exit各自退出效果。

  • return:返回到调用者那里去。
  • pthread_exit():将调用该函数的线程
  • exit: 将进程退出

线程退出

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 
 6 void *thr(void *arg)
 7 {
 8     printf("I am a thread! pid = %d, tid = %lu\n", getpid(), pthread_self());
 9     //return NULL;
10     pthread_exit(NULL);
11     //exit(1);
12 }
13 
14 int main()
15 {
16     pthread_t tid;
17     pthread_create(&tid, NULL, thr, NULL);
18     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
19 
20     sleep(10);
21     printf("I will out.\n");
22 
23     pthread_exit(NULL);
24 
25     return 0;
26 }

    (4)pthread_join函数

    阻塞等待线程退出,获取线程退出状态 其作用,对应进程中 waitpid() 函数。

int pthread_join(pthread_t thread, void **retval); 成功:0;失败:错误号

    参数:

            thread:线程ID (注意:不是指针)

            retval:存储线程结束状态

    对比记忆:

    进程中:main返回值、exit参数-->int;等待子进程结束 wait 函数参数-->int *

    线程中:线程主函数返回值、pthread_exit-->void *;等待线程结束 pthread_join 函数参数-->void **

    练习:参数 retval 非空用法。

pthreat_join接收返回值

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 
 6 void *thr(void *arg)
 7 {
 8     printf("I am a thread! pid = %d, tid = %lu\n", getpid(), pthread_self());
 9     //return (void*)100; //这样退出线程也可以
10     pthread_exit((void *)100);
11 }
12 
13 int main()
14 {
15     pthread_t tid;
16     pthread_create(&tid, NULL, thr, NULL);
17     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
18 
19     void *ret = NULL;
20     pthread_join(tid, &ret); //阻塞等待
21 
22     printf("ret exit with %d\n", (int)ret);
23 
24     pthread_exit(NULL);
25 
26     return 0;
27 }

    调用该函数的线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的,总结如下:

  • 如果thread线程通过return返回,retval所指向的单元里存放的是thread线程函数的返回值。
  • 如果thread线程被别的线程调用pthread_cancel异常终止掉,retval所指向的单元里存放的是常数PTHREAD_CANCELED。
  • 如果thread线程是自己调用pthread_exit终止的,retval所指向的单元存放的是传给pthread_exit的参数。
  • 如果对thread线程的终止状态不感兴趣,可以传NULL给retval参数。

    练习:使用pthread_join函数将循环创建的多个子线程回收。

创建多个子线程并回收

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 void *thr(void *arg)
 8 {
 9     int num = (int)arg;
10     printf("I am %d thread, self = %lu\n", num, pthread_self());
11 
12     return (void *)(100 + num);
13 }
14 
15 int main()
16 {
17     pthread_t tid[5];
18     int i = 0;
19     for (i = 0; i < 5; i++)
20     {
21         pthread_create(&tid[i], NULL, thr, (void*)i);
22     }
23 
24     for (i = 0; i < 5; i++)
25     {
26         void *ret;
27         pthread_join(tid[i], &ret);
28         printf("i = %d, ret = %d\n", i, (int)ret);
29     }
30 
31     return 0;
32 }

    (5)pthread_detach函数

    实现线程分离

int pthread_detach(pthread_t thread); 成功:0;失败:错误号

    线程分离状态:指定该状态,线程主动与主控线程断开关系。线程结束后,其退出状态不由其他线程获取,而直接自己自动释放。网络、多线程服务器常用。

    进程若有该机制,将不会产生僵尸进程。僵尸进程的产生主要由于进程死后,大部分资源被释放,一点残留资源仍存于系统中,导致内核认为该进程仍存在。

    也可使用 pthread_create函数参2(线程属性)来设置线程分离。

    练习:使用pthread_detach函数实现线程分离 。

pthread_detach线程分离

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 void *thr(void *arg)
 8 {
 9     printf("I am a thread, self = %lu\n", pthread_self());
10     sleep(4);
11     printf("I am a thread, self = %lu\n", pthread_self());
12 
13     return NULL;
14 }
15 
16 int main()
17 {
18     pthread_t tid;
19     pthread_create(&tid, NULL, thr, NULL);
20     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
21 
22     pthread_detach(tid); //线程分离
23 
24     sleep(5);
25 
26     //设置线程分离,如果再使用pthread_join回收线程会报错
27     /*
28     int ret = 0;
29     if ((ret = pthread_join(tid, NULL)) > 0) //阻塞等待
30     {
31         printf("join err: %d, %s\n", ret, strerror(ret));
32     }*/
33 
34     return 0;
35 }

    一般情况下,线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的状态为止。但是线程也可以被置为detach状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留

终止状态。不能对一个已经处于detach状态的线程调用pthread_join,这样的调用将返回EINVAL错误。也就是说,如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。

    (6)pthread_cancel函数

    杀死(取消)线程 其作用,对应进程中 kill() 函数。

int pthread_cancel(pthread_t thread); 成功:0;失败:错误号

    注意:线程的取消并不是实时的,而有一定的延时。需要等待线程到达某个取消点(检查点)。

    类似于玩游戏存档,必须到达指定的场所(存档点,如:客栈、仓库、城里等)才能存储进度。杀死线程也不是立刻就能完成,必须要到达取消点。

取消点:是线程检查是否被取消,并按请求进行动作的一个位置。通常是一些系统调用creat,open,pause,close,read,write..... 执行命令man 7 pthreads可以查看具备这些取消点的系统调用列

表。也可参阅 APUE.12.7 取消选项小节。

    可粗略认为一个系统调用(进入内核)即为一个取消点。如线程中没有取消点,可以通过调用pthreestcancel函数自行设置一个取消点。

    被取消的线程, 退出值定义在Linux的pthread库中。常数PTHREAD_CANCELED的值是-1。可在头文件pthread.h中找到它的定义:#define PTHREAD_CANCELED ((void *) -1)。因此当我们对一

个已经被取消的线程使用pthread_join回收时,得到的返回值为-1。

    练习:终止线程的三种方法。注意“取消点”的概念。

pthread_cancel线程取消

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 
 6 void *thr(void *arg)
 7 {
 8     while(1)
 9     {
10         //注意:如果没有下面这两行代码,也就是只是一个while(1)死循环,则无法杀死该线程
11         //或者加上pthread_testcancel();设置杀死点,函数也可以被主线程杀死
12         printf("I am a thread! pid = %d, tid = %lu\n", getpid(), pthread_self());
13         sleep(1);
14         //pthread_testcancel();
15     }
16 
17     return NULL;
18 }
19 
20 int main()
21 {
22     pthread_t tid;
23     pthread_create(&tid, NULL, thr, NULL);
24     printf("I am main thread, pid = %d, tid = %lu\n", getpid(), pthread_self());
25 
26     sleep(5);
27     pthread_cancel(tid); //杀死线程
28 
29     void *ret;
30     pthread_join(tid, &ret); //阻塞等待
31     printf("thread exit with %d\n", (int)ret);
32 
33     return 0;
34 }

    终止线程方式:

    总结:终止某个线程而不终止整个进程,有三种方法:

  • 从线程主函数return。这种方法对主控线程不适用,从main函数return相当于调用exit。
  • 一个线程可以调用pthread_cancel终止同一进程中的另一个线程。
  • 线程可以调用pthread_exit终止自己。

    (7)pthread_equal函数

    比较两个线程ID是否相等。

int pthread_equal(pthread_t t1, pthread_t t2);

    有可能Linux在未来线程ID pthread_t 类型被修改为结构体实现。

 4. 控制原语对比

进程           线程
 fork      pthread_create
 exit      pthread_exit
 wait      pthread_join
 kill      pthread_cancel
 getpid    pthread_self

5. 线程属性

    本节作为指引性介绍,linux下线程的属性是可以根据实际项目需要,进行设置,之前我们讨论的线程都是采用线程的默认属性,默认属性已经可以解决绝大多数开发时遇到的问题。如我们对程序的性能提出更高的要求那么需要设置线程属性,比如可以通过设置线程栈的大小来降低内存的使用,增加最大线程个数。

typedef struct
{
    int etachstate; //线程的分离状态
    int schedpolicy; //线程调度策略
    struct sched_param schedparam; //线程的调度参数
    int inheritsched; //线程的继承性
    int scope; //线程的作用域
    size_t guardsize; //线程栈末尾的警戒缓冲区大小
    int stackaddr_set; //线程的栈设置
    void* stackaddr; //线程栈的位置
    size_t stacksize; //线程栈的大小
} pthread_attr_t;

    主要结构体成员:

  • 线程分离状态
  • 线程栈大小(默认平均分配)
  • 线程栈警戒缓冲区大小(位于栈末尾) 参 APUE.12.3 线程属性

    属性值不能直接设置,须使用相关函数进行操作,初始化的函数为pthread_attr_init,这个函数必须在pthread_create函数之前调用。之后须用pthread_attr_destroy函数来释放资源。

    线程属性主要包括如下属性:作用域(scope)、栈尺寸(stack size)、栈地址(stack address)、优先级(priority)、分离的状态(detached state)、调度策略和参数(scheduling policy and

parameters)。默认的属性为非绑定、非分离、缺省的堆栈、与父进程同样级别的优先级。

    1)线程属性初始化

    注意:应先初始化线程属性,再pthread_create创建线程

    初始化线程属性

int pthread_attr_init(pthread_attr_t *attr); 成功:0;失败:错误号

    销毁线程属性所占用的资源

int pthread_attr_destroy(pthread_attr_t *attr); 成功:0;失败:错误号

    2)线程的分离状态

    线程的分离状态决定一个线程以什么样的方式来终止自己。

    非分离状态:线程的默认属性是非分离状态,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。

    分离状态:分离线程没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。应该根据自己的需要,选择适当的分离状态。

    线程分离状态的函数:

    设置线程属性,分离or非分离

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

    获取程属性,分离or非分离

int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);

    参数:

            attr:已初始化的线程属性

            detachstate: PTHREAD_CREATE_DETACHED(分离线程)

                                   PTHREAD _CREATE_JOINABLE(非分离线程)

    这里要注意的一点是,如果设置一个线程为分离线程,而这个线程运行又非常快,它很可能在pthread_create函数返回之前就终止了,它终止以后就可能将线程号和系统资源移交给其他的线程使

用,这样调用pthread_create的线程就得到了错误的线程号。要避免这种情况可以采取一定的同步措施,最简单的方法之一是可以在被创建的线程里调用pthread_cond_timedwait函数,让这个线程等

待一会儿,留出足够的时间让函数pthread_create返回。设置一段等待时间,是在多线程编程里常用的方法。但是注意不要使用诸如wait()之类的函数,它们是使整个进程睡眠,并不能解决线程同步

的问题。

    3)线程的栈地址

    POSIX.1定义了两个常量_POSIX_THREAD_ATTR_STACKADDR 和_POSIX_THREAD_ATTR_STACKSIZE检测系统是否支持栈属性。也可以给sysconf函数传递_SC_THREAD_ATTR_STACKADDR或 _SC_THREAD_ATTR_STACKSIZE来进行检测。

    当进程栈地址空间不够用时,指定新建线程使用由malloc分配的空间作为自己的栈空间。通过pthread_attr_setstack和pthread_attr_getstack两个函数分别设置和获取线程的栈地址。

int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); 成功:0;失败:错误号
int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize); 成功:0;失败:错误号

    参数:

           attr:指向一个线程属性的指针

           stackaddr:返回获取的栈地址

           stacksize:返回获取的栈大小

    4)线程的栈大小

    当系统中有很多线程时,可能需要减小每个线程栈的默认大小,防止进程的地址空间不够用,当线程调用的函数会分配很大的局部变量或者函数调用层次很深时,可能需要增大线程栈的默认大小。

    函数pthread_attr_getstacksize和 pthread_attr_setstacksize提供设置。

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); 成功:0;失败:错误号
int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize); 成功:0;失败:错误号

    参数:

            attr:指向一个线程属性的指针

            stacksize:返回线程的堆栈大小

    5)线程属性控制示例

不分离

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 void *thr(void *arg)
 8 {
 9     printf("I am a thread\n");
10     return NULL;
11 }
12 
13 int main()
14 {
15     pthread_attr_t attr;
16     pthread_attr_init(&attr);
17 
18     pthread_t tid;
19     pthread_create(&tid, &attr, thr, NULL);
20 
21     int ret;
22     if ((ret = pthread_join(tid, NULL)) > 0)
23     {
24         printf("join err: %d, %s\n", ret, strerror(ret));
25         return -1;
26     }
27     pthread_attr_destroy(&attr); //摧毁属性
28 
29     return 0;
30 }

分离

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 void *thr(void *arg)
 8 {
 9     printf("I am a thread\n");
10     return NULL;
11 }
12 
13 int main()
14 {
15     pthread_attr_t attr;
16     pthread_attr_init(&attr);
17 
18     //设置属性分离,不需要通过pthread_join回收
19     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
20 
21     pthread_t tid;
22     pthread_create(&tid, &attr, thr, NULL);
23 
24     /*
25     int ret;
26     if ((ret = pthread_join(tid, NULL)) > 0)
27     {
28         printf("join err: %d, %s\n", ret, strerror(ret));
29         return -1;
30     }*/
31     pthread_attr_destroy(&attr); //摧毁属性
32 
33     sleep(1);
34 
35     return 0;
36 }

综合示例

1 #include <pthread.h>
 2 
 3 #define SIZE 0x100000
 4 
 5 void *th_fun(void *arg)
 6 {
 7     while(1)
 8     {
 9         sleep(1);
10     }
11 }
12 
13 int main(void)
14 {
15     pthread_t tid;
16     int err, detachstate, i = 1;
17     pthread_attr_t attr;
18     void *stackaddr;
19     size_t stacksize;
20 
21     pthread_attr_init(&attr);
22     pthread_attr_getstack(&attr, &stackaddr, &stacksize);
23     pthread_attr_getdetachstate(&attr, &detachstate);
24 
25     if (detachstate == PTHREAD_CREATE_DETACHED)
26     {
27         printf("thread detached\n");
28     }
29     else if (detachstate == PTHREAD_CREATE_JOINABLE)
30     {
31         printf("thread join\n");
32     }
33     else
34     {
35         printf("thread unknown\n");
36     }
37 
38     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
39 
40     while (1) 
41     {
42 
43         stackaddr = malloc(SIZE);
44         if (stackaddr == NULL) 
45         {
46             perror("malloc");
47             exit(1);
48         }
49 
50         stacksize = SIZE;
51         pthread_attr_setstack(&attr, stackaddr, stacksize);
52         err = pthread_create(&tid, &attr, th_fun, NULL);
53         if (err != 0) 
54         {
55             printf("%s\n", strerror(err));
56             exit(1);
57         }
58         printf("%d\n", i++);
59     }
60 
61     pthread_attr_destroy(&attr);
62     
63     return 0;
64 }

    6)NPTL

  • 察看当前pthread库版本getconf GNU_LIBPTHREAD_VERSION
  • NPTL实现机制(POSIX),Native POSIX Thread Library
  • 使用线程库时gcc指定 –lpthread

 6. 线程使用注意事项

    主线程退出其他线程不退出,主线程应调用pthread_exit
    避免僵尸线程

  • pthread_join
  • pthread_detach
  • pthread_create指定分离属性

    被join线程可能在join函数返回前就释放完自己的所有内存资源,所以不应当返回被回收线程栈中的值;

    malloc和mmap申请的内存可以被其他线程释放。

    应避免在多线程模型中调用fork除非,马上exec,子进程中只有调用fork的线程存在,其他线程在子进程中均pthread_exit。

    信号的复杂语义很难和多线程共存,应避免在多线程引入信号机制。

练习:

1. 实现一个守护进程,每分钟吸入一次日志,要去日志保存在$HOME/log/下,

  • 命名规则:程序名.yyyymm
  • 写入内容格式:mm:dd hh:mi:ss程序名 [进程号]:消息内容

2. 实现多线程拷贝。(利用mmap映射区)


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200