zoukankan      html  css  js  c++  java
  • linux C--多线程基本概念及API函数

    一、县城和进程之间的关系,与进程相比线程有哪些优点?

    1、线程与进程之间的关系:

    (1)线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。(2) 一个进程由几个线程组成,线程与同属一个进程的其它线程共享进程所拥有的全部资源。(3)进程--资源分配的最小单位,线程--程序执行的最小单位

    (4)进程有独立的地址空间,一个进程崩溃之后,在保护模式下下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。

    (5)线程有自己的堆栈和局部变量,单线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率差一点。

    2、与进程相比有哪些优点:

    (1)是一种非常节俭的多任务操作方式,在linux下,启动一个新的进程必须分配给他独立的地址空间,建立众多的数据表来维护它的代码段,堆栈段,数据段,这是一种昂贵的多任务工作方式。

    (2)线程间方便地通信机制。

    (3)使多CPU系统更加的高效,操作系统会保证当前线程数不大于cpu数目时,不同的线程运行于不同的cpu上。

    (4)改善程序结构,一个即长又复杂的进程可以考虑分成多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。

    二、现成的创建,等待,退出,的api函数详解。

    1、线程创建:pthread_create()

    int pthread_create( pthread_t * tidp, const pthread_attr_t * attr,(void *)(*start_rtn)(void*),void* arg);

    参数:tidp :为指向线程标识符的指针。

    attr:用来设置线程属性。

    arg:传入线程执行函数的参数

    成功返回 0 ,失败 返回 错误码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #include <stdio.h> 
    #include <pthread.h> 
     
    void *myThread1(void
        int i; 
        for (i=0; i<3; i++) 
        
            printf("This is the 1st pthread,created by zieckey. "); 
            sleep(1);//Let this thread to sleep 1 second,and then continue to run 
        
     
    void *myThread2(void
        int i; 
        for (i=0; i<3; i++) 
        
            printf("This is the 2st pthread,created by zieckey. "); 
            sleep(1); 
        
     
    int main() 
        int i=0, ret=0
        pthread_t id1,id2; 
     
        /*创建线程1*/
        ret = pthread_create(&id1, NULL, (void*)myThread1, NULL); 
        if (ret) 
        
            printf("Create pthread error! "); 
            return 1
        
     
        /*创建线程2*/
        ret = pthread_create(&id2, NULL, (void*)myThread2, NULL); 
        if (ret) 
        
            printf("Create pthread error! "); 
            return 1
        
     
        pthread_join(id1, NULL); 
        pthread_join(id2, NULL); 
     
        return 0
    }

    2、线程等待退出:pthread_join()

    int pthread_join(pthread_t thread ,void ** retval);

    参数:thread线程标识符,就是线程id,唯一的标识线程。

    retval:用户定义的指针,用来存储被等待的线程的返回值。

    返回值:0 代表成功,失败 返回的是 错误号

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #include <pthread.h> 
    #include <unistd.h> 
    #include <stdio.h> 
     
    void *thread(void *str) 
        int i; 
        for (i = 0; i < 10; ++i) 
        
            sleep(2); 
            printf( "This in the thread : %d " , i ); 
        
        return NULL; 
     
    int main() 
        pthread_t pth; 
        int i; 
        int ret = pthread_create(&pth, NULL, thread, (void *)(i)); 
     
        pthread_join(pth, NULL); 
     
        printf("123 "); 
        for (i = 0; i < 10; ++i) 
        
            sleep(1); 
            printf( "This in the main : %d " , i ); 
        
     
        return 0
    }

    3、线程退出:pthread_exit()

    void pthread_exit(void *retval)

    返回值: 调用现成的返回值,可用其它函数pthread_join 来获取

    查看线程的返回值:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    #include <stdio.h> 
    #include <pthread.h> 
    #include <unistd.h> 
     
    void *create(void *arg) 
        printf("new thread is created ... "); 
        return (void *)8
     
    int main(int argc,char *argv[]) 
        pthread_t tid; 
        int error; 
        void *temp; 
     
        error = pthread_create(&tid, NULL, create, NULL); 
        printf("main thread! "); 
        if( error ) 
        
            printf("thread is not created ... "); 
            return -1
        
        error = pthread_join(tid, &temp); 
        if( error ) 
        
            printf("thread is not exit ... "); 
            return -2
        
     
        printf("thread is exit code %d ", (int)temp); 
        return 0
    }

    三、在创建线程时参数如何传递:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #include <stdio.h> 
    #include <pthread.h> 
    #include <unistd.h> 
    #include <stdlib.h> 
     
    struct menber 
        int a; 
        char *s; 
    }; 
     
    void *create(void *arg) 
        struct menber *temp; 
        temp=(struct menber *)arg; 
        printf("menber->a = %d  ",temp->a); 
        printf("menber->s = %s  ",temp->s); 
     
        return (void *)0
     
     
    int main(int argc,char *argv[]) 
        pthread_t tidp; 
        int error; 
        struct menber *b; 
     
        b=(struct menber *)malloc( sizeof(struct menber) ); 
        b->a = 4
        b->s = "zieckey"
     
        error = pthread_create(&tidp, NULL, create, (void *)b); 
     
        if( error ) 
        
            printf("phread is not created... "); 
            return -1
        
        sleep(1); 
        printf("pthread is created... "); 
        return 0
    }

     

  • 相关阅读:
    Mvc5+Ef 6.0入门开发随笔(2.MVC的简单创建与使用图文详解)
    转载:CSDN mvc ef 的简单增删改查操作
    插件
    Jmeter-后置处理器--json提取器
    jmeter的几种参数化方式
    Python--变量命名规范
    Python--对list、tuple、dict的操作
    Python--列表中字符串按照某种规则排序的方法
    Python--遍历文件夹下所有文件和目录的方法(os.walk(rootdir)函数返回一个三元素元祖)
    Python--递归函数实现:多维嵌套字典数据无限遍历
  • 原文地址:https://www.cnblogs.com/yjds/p/8598875.html
Copyright © 2011-2022 走看看