亚洲精品一二区_国产黄色片网站_99久久久成人国产精品_蜜臀网_国产精品一区二区三区免费_成人av中文字幕_91精品国产欧美一区二区成人

Hi,歡迎來到嵌入式培訓高端品牌 - 華清遠見教育科技集團<北京總部官網>,專注嵌入式工程師培養(yǎng)15年!
當前位置: > 華清遠見教育科技集團 > 嵌入式學習 > 講師博文 > 線程解析(一)
線程解析(一)
時間:2016-12-30作者:華清遠見

在使用線程之前我們要知道線程是個怎樣的概念,它的作用是什么?

在學多進程或多線程編程的時候,有這樣一個概念----進程是資源管理的小單位,線程是程序執(zhí)行的小單位,相對于進程,線程上下文切換花費的開銷更少。因為線程共享了內存空間,不同線程可以訪問同一個變量,即多有線程都可以訪問已經聲明了的全局變量。在多進程中這種情況需要使用IPC(進程間通信)對象實現,這樣增加了額外的開銷,從而降低了性能,并且增加了代碼的復雜度。

在執(zhí)行方面線程也有著它的優(yōu)勢,因為在創(chuàng)建線程的過程中系統(tǒng)無需復制內存空間和文件描述符等,這樣節(jié)省了很多的cpu時間。

首先我們了解幾個相關函數
        pthread_create()創(chuàng)建線程,函數原型為:
        int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, 
        void *(*start_routine)(void*), void *restrict arg);
        thread 返回創(chuàng)建線程的ID,attr是創(chuàng)建線程是設置的線程屬性。start_routine是線程執(zhí)行體函數,arg為傳遞到線程執(zhí)行體函數的一個參數。

下面我們寫一個簡單的多線程的程序。

pthread1.c
        #include <stdio.h>
        #include <pthread.h>

void *thread_a(void *arg)
    {
        printf("thread a enter\n");
    }

void *thread_b(void *arg)
    {
        printf("thread b enter\n");
    }

int main(int argc, char **argv)
    {
        pthread_t tid_a,tid_b;
        int err;
        err = pthread_create(&tid_a,NULL,thread_a,NULL);
        if(err < 0)
        {
            perror("pthread_create thread_a");
        }

err = pthread_create(&tid_b,NULL,thread_b,NULL);
        if(err < 0)
        {
            perror("pthread_create thread_a");
        }
        sleep(5);
        printf("the main close\n");
        return 0;
    }

在這個程序中我們創(chuàng)建了兩個線程分別是tid_a和tid_b,thread_a和thread_b分別是他們的執(zhí)行體。這個時候我們的程序中有了三個線程。因為除了我們常見的兩個線程外,主程序也是一個線程。下面我們嘗試一下傳遞一個參數到線程執(zhí)行體中。
    pthread2.c
    #include <stdio.h>
    #include <pthread.h>

void *thread_a(void *arg)
    {
        printf("thread a = %d enter\n",*(int *)arg);
    }

void *thread_b(void *arg)
    {
        printf("thread b = %d enter\n", *(int *)arg);
    }

int main(int argc, char **argv)
    {
        pthread_t tid_a,tid_b;
        int err;
        err = pthread_create(&tid_a,NULL,thread_a,(void *)&tid_a);
        if(err < 0)
        {
            perror("pthread_create thread_a");
        }
        printf("create tid_a = %d\n",tid_a);
        err = pthread_create(&tid_b,NULL,thread_b,(void *)&tid_b);
        if(err < 0)
        {
            perror("pthread_create thread_a");
        }
        printf("create tid_b = %d\n",tid_b);
        sleep(5);
        printf("the main close\n");
        return 0;
    }

發(fā)表評論
評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
主站蜘蛛池模板: 国产亚洲精品aa在线观看 | 啪啪国产 | 深夜福利日韩 | 国产成人一区二区三区精品久久 | 日本视频在线观看不卡高清免费 | 这里只有精品99re在线 | 精品一卡2卡三卡4卡免费观看 | 99视频在线免费 | 久伊人| 九九久久精品这里久久网 | 亚洲高清免费在线观看 | 波多野结衣国产一区二区三区 | www.日本视频 | 免费日本在线 | 欧美精品 在线观看 | 在线观看日本一区二区 | 秋霞韩国理论片观看 | 三级毛片视频 | 成人在线网址 | 免费观看精品视频999 | 欧美久久久久久 | 一级视频在线免费观看 | 国产成人av在线 | 日本jizzz | 欧美一区二区三区香蕉视 | 国产精品视频二区不卡 | 日本人69视频页码jlzz | www.99色| 免费一级大片儿 | 日本黄视频网站 | 免费观看欧美一区二区三区 | 久久精品9 | aa在线播放| 亚洲成a人 | 人人爱天天做夜夜爽2020麻豆 | 日本不卡免费新一二三区 | 国产亚洲3p无码一区二区 | 亚洲国产欧美在线成人aaaa | 欧美一级日韩一级亚洲一级va | 欧美另类视频一区二区三区 | 精品久久久久久久久久香蕉 |