• Linux下多线程编程之——线程竞争


    一、多线程竞争域

      1、函数pthread_attr_setscope()和函数pthread_attr_getscope()用来设置和获取线程的作用域,作用域决定线程是在进程内还是在系统内竞争资源,linux下只能设置为:PTHREAD_SCOPE_SYSTEM

    二、代码test7_6.c

     1 //This is c program code!                                                                                                                      
     2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
     3   * 文档信息: *** :~/test7_6.c
     4   * 版权声明: *** :(魎魍魅魑)MIT
     5   * 联络信箱: *** :guochaoxxl@163.com
     6   * 创建时间: *** :2020年11月17日的下午08:49
     7   * 文档用途: *** :数据结构与算法分析-c语言描述
     8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
     9   * 修订时间: *** :2020年第46周 11月17日 星期二 下午08:49 (第322天)
    10   * 文件描述: *** :自行添加
    11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
    12 #include <stdio.h>
    13 #include <pthread.h>
    14 #include <errno.h>
    15 
    16 void *myComadd(void *iData){
    17     int sum = 0;
    18     int *data = (int *)(iData);
    19     int contScope; 
    20     pthread_attr_t attr;
    21     pthread_getattr_np(pthread_self(), &attr);
    22     pthread_attr_getscope(&attr, &contScope);
    23     
    24     if(contScope == PTHREAD_SCOPE_SYSTEM){
    25         printf("myComadd系统竞争!
    ");
    26     }
    27     if(contScope == PTHREAD_SCOPE_PROCESS){
    28         printf("myComadd进程竞争!
    ");
    29     }
    30 
    31     for(int i = 0; i < *data; i++){
    32         sum += i;
    33     }
    34     
    35     printf("add %d
    ", sum);
    36 }
    37 
    38 void *myCommul(void *iData){
    39     int sum = 1;
    40     int *data = (int *)(iData);
    41     int contScope;
    42     pthread_attr_t attr;
    43     pthread_getattr_np(pthread_self(), &attr);
    44     pthread_attr_getscope(&attr, &contScope);
    45     
    46     if(contScope == PTHREAD_SCOPE_SYSTEM){
    47         printf("myComadd系统竞争!
    ");
    48     }
    49     if(contScope == PTHREAD_SCOPE_PROCESS){
    50         printf("myComadd进程竞争!
    ");
    51     }
    52 
    53     for(int i = 1; i <= *data; i++){
    54         sum *= i;
    55     }
    56 
    57     printf("mul %d
    ", sum);
    58 }
    59 
    60 int main(int argc, char **argv)
    61 {
    62     int n = 5;
    63     pthread_t threadA;
    64     pthread_t threadB;
    65     pthread_attr_t attr;
    66     pthread_attr_init(&attr);
    67 
    68     pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);                                                                                        
    69     if(pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS) == ENOTSUP){
    70         printf("Linux系统不支持系统调度竞争
    ");
    71     }
    72 
    73     pthread_create(&threadA, NULL, myComadd, &n);
    74     pthread_create(&threadB, &attr, myCommul, &n);
    75 
    76     sleep(1);
    77 
    78     return 0;
    79 }   

      代码很简单,不做说明!

  • 相关阅读:
    蓝桥杯之递归算法基本框架

    Dubbo是什么
    java
    java
    java
    java
    java
    负载均衡的理解
    设计模式学习
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/14002484.html
Copyright © 2020-2023  润新知