• POSIX线程


    posix线程库接口:
    引入<pthread.h> 链接要加上-lpthread

    成功返回0,失败返回错误码.pthread函数出错时不会设置全局变量errno,而是将错误代码返回。
    int pthread_create(pthread_t* thread,const pthread_attr_t* attr,void*(*start_routine)(void*),void* arg);
    thread:返回的线程ID。
    attr:设置线程属性,NULL默认属性
    start_routine:是个函数地址,线程启动后要执行的函数。接受无类型指针参数,返回无类型指针
    arg:传给线程启动函数的参数

    pid_t                  thread_t
    fork                    pthread_create
    waitpid               pthread_join
    exit                    pthread_exit  (退出线程)
    main函数中return             线程入口函数中 return 
    僵进程          僵线程(线程结束了,但是没有调用pthread_join的话)
    waitpid                               pthread_join()


    将线程设置为脱离的,则这个线程就不会变成疆线程:pthread_detach(分离线程,避免僵死)


    线程结束:自杀 pthread_exit return       他杀,杀掉另外一个线程,可以类比为进程中的kill函数:pthread_cancel


    等待新创建线程的结束
      int pthread_join(pthread_t thread, void **retval);
    结束线程
      void pthread_exit(void *retval);

     1 #include<unistd.h>
     2 #include<sys/types.h>
     3 #include<fcntl.h>
     4 #include<sys/stat.h>
     5 #include<stdlib.h>
     6 #include<stdio.h>
     7 #include<errno.h>
     8 #include<pthread.h>
     9 #include<string.h>
    10 #define ERR_EXIT(m)
    11     do
    12     {
    13         perror(m);
    14         exit(EXIT_FAILURE);
    15     }while(0)
    16 //pthread同样也提供了线程内errno变量,以支持其他使用errno的代码。对于pthread函数的错误。建议通过返回值判断因为读取返回值要比读取线程内的errno变量开销更小
    17 void* thread_routine(void* arg)
    18 {
    19     int i;
    20     for(i=0;i<20;i++)
    21     {
    22         printf("B");
    23         fflush(stdout);
    24 /*        if(i==3)
    25             pthread_exit("ABC");//运行4遍退出
    26 */
    27         
    28     }
    29     return "DEF";//退出信息也会传到pthread_join的第二个参数
    30 }
    31 int main(void)
    32 {
    33 
    34     pthread_t tid;
    35     int ret;
    36     
    37     if((ret=pthread_create(&tid,NULL,thread_routine,NULL))!=0)
    38     {
    39         fprintf(stderr,"pthread_create:%s
    ",strerror(ret));//将错误信息,将errno转换为错误信息。
    40         exit(EXIT_FAILURE);
    41     }
    42     int i=0;
    43     //主线程和新线程交替运行    
    44     for(i=0;i<20;i++)
    45     {
    46         printf("A");
    47         fflush(stdout);
    48     }
    49     /*
    50     //如果不等待,主线程可能结束,新线程的B输出不完整
    51     sleep(1);
    52     */
    53     void* value;
    54     if((ret=pthread_join(tid,&value))!=0)
    55     {
    56         fprintf(stderr,"pthread_join:%s
    ",strerror(ret));
    57         exit(EXIT_FAILURE);
    58     }
    59     printf("
    ");
    60     printf("return msg=%s
    ",(char*)value);
    61     return 0;
    62 }
  • 相关阅读:
    kvm
    Javascript 笔记与总结(2-7)对象
    [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
    Swift5.3 语言指南(五) 基本运算符
    C#6.0语言规范(一) 介绍
    [Swift]LeetCode171. Excel表列序号 | Excel Sheet Column Number
    [Swift]LeetCode169. 求众数 | Majority Element
    [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
    [Swift]LeetCode167. 两数之和 II
    [Java]LeetCode141. 环形链表 | Linked List Cycle
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/8495075.html
Copyright © 2020-2023  润新知