Linux ---- 多线程

  头文件: #include <pthread.h>
1. pthread_create
    一般情况下,main函数所在的线程,称之为主线程(main线程),其余创建的线程称之为子线程。
      程序中默认只有一个进程,fork()函数调用,变成2个进程
      程序中默认只有一个线程,pthread_create()函数调用,变成2个线程
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
        - 功能:创建一个子线程
        - 参数:
            - thread:传出参数;线程创建成功后,子线程的线程ID被写到该变量中。
            - attr : 设置线程的属性,一般使用默认值,NULL
            - start_routine : 函数指针,这个函数是子线程需要处理的逻辑代码
            - arg : 给第三个参数使用,传参
        - 返回值:
            成功:0
            失败:返回错误号。这个错误号和之前errno不同
            获取错误号的信息:  char * strerror(int errnum);
    注意:Compile and link with -pthread(使用 -pthread 编译和链接)
       gcc pthread_creat.c -o creat -pthead
  
复制代码
 1 #include <pthread.h>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 
 6 void *callback(void *arg) {
 7     // 子线程的代码区
 8     printf("child thread...\n");
 9     printf("arg value: %d\n", *( int * )arg);
10     return NULL;
11 }
12 
13 int main() {
14     // 主线程的代码区
15     pthread_t tid;
16 
17     int num = 10;
18 
19     // 创建一个子线程
20     // int ret = pthread_create(&tid, NULL, callback, NULL);
21     // 将num传入了callback函数内
22     int ret = pthread_create(&tid, NULL, callback, ( void * )&num);
23 
24     if (ret != 0) {
25         char *errstr = strerror(ret);
26         printf("error : %s\n", errstr);
27     }
28 
29     for (int i = 0; i < 5; i++) {
30         printf("%d\n", i);
31     }
32 
33     sleep(1);
34 
35     return 0;    // exit(0);
36     // 主线程退出,子线程也退出
37 }
复制代码

2. pthread_exit

     void pthread_exit(void *retval);
        功能:终止一个线程,在哪个线程中调用,就表示终止哪个线程
        参数:
            retval:需要传递一个指针,作为一个返回值,可以在pthread_join()中获取到。
 
3. pthread_self
    pthread_t pthread_self(void);
        功能:获取当前线程tid
 
4. pthread_equal
    int pthread_equal(pthread_t t1, pthread_t t2);
        功能:比较两个线程ID是否相等
        不同的操作系统,pthread_t类型的实现不一样,有的是无符号的长整型,有的是使用结构体去实现的。
复制代码
 1 #include <pthread.h>
 2 #include <stdio.h>
 3 #include <string.h>
 4 
 5 void *callback(void *arg) {
 6     // 获取子进程的 id
 7     printf("chlid thread id: %ld\n", pthread_self());
 8     return NULL;    // 返回空值 = pthread_exit(NULL);
 9 }
10 int main() {
11     pthread_t tid;
12 
13     int ret = pthread_create(&tid, NULL, callback, NULL);
14     if (ret != 0) {
15         char *str = strerror(ret);
16         printf("%s", str);
17     }
18 
19     for (int i = 0; i < 5; i++) {
20         printf("i = %d\n", i);
21     }
22 
23     printf("tid : %ld, main thread id: %ld\n", tid, pthread_self());
24 
25     // 主线程退出,不会影响子线程
26     pthread_exit(NULL);
27     // 后面的代码没有执行
28     return 0;
29 }
复制代码

5. pthread_join

  int pthread_join(pthread_t thread, void **retval);
        - 功能:和一个已经终止的线程进行连接,用来回收子线程的资源,为阻塞函数,调用一次只能回收一个子线程,一般在主线程中使用
        - 参数:
            - thread:需要回收的子线程的ID
            - retval: 接收子线程退出时的返回值
        - 返回值:
            成功: 0
            失败: 非0, 返回的错误号。

 6. pthread_detach

  int pthread_detach(pthread_t thread);
        - 功能:分离一个线程。被分离的线程在终止的时候,会自动释放资源返回给系统。
          1.不能多次分离,会产生不可预料的行为。
          2.不能去连接一个已经分离的线程,会报错。
        - 参数:需要分离的线程的ID
        - 返回值:
            成功:0
            失败:返回错误号
7. thread_cancel 
  int pthread_cancel(pthread_t thread);
        - 功能:取消线程(让线程终止)
            取消某个线程,可以终止某个线程的运行,但是并不是立马终止,而是当子线程执行到一个取消点,线程才会终止。
            取消点:系统规定好的一些系统调用,我们可以粗略的理解为从用户区到内核区的切换,这个位置称之为取消点。
复制代码
 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 
 6 void * callback(void * arg) {
 7     printf("chid thread id : %ld\n", pthread_self());
 8     return NULL;
 9 }
10 
11 int main() {
12 
13     // 创建一个线程属性变量
14     pthread_attr_t attr;
15     // 初始化属性变量
16     pthread_attr_init(&attr);
17 
18     // 设置属性
19     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
20 
21     // 创建一个子线程
22     pthread_t tid;
23 
24     int ret = pthread_create(&tid, &attr, callback, NULL);
25     if(ret != 0) {
26         char * errstr = strerror(ret);
27         printf("error1 : %s\n", errstr);
28     }
29 
30     // 获取线程的栈的大小
31     size_t size;
32     pthread_attr_getstacksize(&attr, &size);
33     printf("thread stack size : %ld\n", size);
34 
35     // 输出主线程和子线程的id
36     printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
37 
38     // 释放线程属性资源
39     pthread_attr_destroy(&attr);
40 
41     pthread_exit(NULL);
42 
43     return 0;
44 }
复制代码

 

 

posted @   Happinesspill  阅读(222)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示