日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 多線程編程之:Linux線程編程

          多線程編程之:Linux線程編程

          作者: 時間:2014-10-17 來源:網(wǎng)絡(luò) 收藏

            9.2.3 線程屬性

          本文引用地址:http://yuyingmama.com.cn/article/264053.htm

            (1)函數(shù)說明。

            pthread_create()函數(shù)的第二個參數(shù)(pthread_attr_t *attr)表示線程的屬性。在上一個實例中,將該值設(shè)為NULL,也就是采用默認(rèn)屬性,線程的多項屬性都是可以更改的。這些屬性主要包括綁定屬性、分離屬性、堆棧地址、堆棧大小以及優(yōu)先級。其中系統(tǒng)默認(rèn)的屬性為非綁定、非分離、缺省1M的堆棧以及與父進(jìn)程同樣級別的優(yōu)先級。下面首先對綁定屬性和分離屬性的基本概念進(jìn)行講解。

            n 綁定屬性。

            前面已經(jīng)提到,中采用“一對一”的線程機(jī)制,也就是一個用戶線程對應(yīng)一個內(nèi)核線程。綁定屬性就是指一個用戶線程固定地分配給一個內(nèi)核線程,因為CPU時間片的調(diào)度是面向內(nèi)核線程(也就是輕量級進(jìn)程)的,因此具有綁定屬性的線程可以保證在需要的時候總有一個內(nèi)核線程與之對應(yīng)。而與之對應(yīng)的非綁定屬性就是指用戶線程和內(nèi)核線程的關(guān)系不是始終固定的,而是由系統(tǒng)來控制分配的。

            n 分離屬性。

            分離屬性是用來決定一個線程以什么樣的方式來終止自己。在非分離情況下,當(dāng)一個線程結(jié)束時,它所占用的系統(tǒng)資源并沒有被釋放,也就是沒有真正的終止。只有當(dāng)pthread_join()函數(shù)返回時,創(chuàng)建的線程才能釋放自己占用的系統(tǒng)資源。而在分離屬性情況下,一個線程結(jié)束時立即釋放它所占有的系統(tǒng)資源。這里要注意的一點是,如果設(shè)置一個線程的分離屬性,而這個線程運(yùn)行又非???,那么它很可能在pthread_create()函數(shù)返回之前就終止了,它終止以后就可能將線程號和系統(tǒng)資源移交給其他的線程使用,這時調(diào)用pthread_create()的線程就得到了錯誤的線程號。

            這些屬性的設(shè)置都是通過特定的函數(shù)來完成的,通常首先調(diào)用pthread_attr_init()函數(shù)進(jìn)行初始化,之后再調(diào)用相應(yīng)的屬性設(shè)置函數(shù),最后調(diào)用pthread_attr_destroy()函數(shù)對分配的屬性結(jié)構(gòu)指針進(jìn)行清理和回收。設(shè)置綁定屬性的函數(shù)為pthread_attr_setscope(),設(shè)置線程分離屬性的函數(shù)為pthread_attr_setdetachstate(),設(shè)置線程優(yōu)先級的相關(guān)函數(shù)為pthread_attr_getschedparam()(獲取線程優(yōu)先級)和pthread_attr_setschedparam()(設(shè)置線程優(yōu)先級)。在設(shè)置完這些屬性后,就可以調(diào)用pthread_create()函數(shù)來創(chuàng)建線程了。

            (2)函數(shù)格式。

            表9.9列出了pthread_attr_init()函數(shù)的語法要點。

            表9.10列出了pthread_attr_setscope()函數(shù)的語法要點。

            表9.11列出了pthread_attr_setdetachstate()函數(shù)的語法要點。

            表9.12 pthread_attr_getschedparam()函數(shù)語法要點

            表9.13列出了pthread_attr_setschedparam()函數(shù)的語法要點。

            (3)使用實例。

            下面的實例是在我們已經(jīng)很熟悉的實例的基礎(chǔ)上增加線程屬性設(shè)置的功能。為了避免不必要的復(fù)雜性,這里就創(chuàng)建一個線程,這個線程具有綁定和分離屬性,而且主線程通過一個finish_flag標(biāo)志變量來獲得線程結(jié)束的消息,而并不調(diào)用pthread_join()函數(shù)。

            /*thread_attr.c*/

            #include

            #include

            #include

            #define REPEAT_NUMBER 3 /* 線程中的小任務(wù)數(shù) */

            #define DELAY_TIME_LEVELS 10.0 /* 小任務(wù)之間的最大時間間隔 */

            int finish_flag = 0;

            void *thrd_func(void *arg)

            {

            int delay_time = 0;

            int count = 0;

            printf("Thread is startingn");

            for (count = 0; count < REPEAT_NUMBER; count++)

            {

            delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;

            sleep(delay_time);

            printf("tThread : job %d delay = %dn", count, delay_time);

            }

            printf("Thread finishedn");

            finish_flag = 1;

            pthread_exit(NULL);

            }

            int main(void)

            {

            pthread_t thread;

            pthread_attr_t attr;

            int no = 0, res;

            void * thrd_ret;

            srand(time(NULL));

            /* 初始化線程屬性對象 */

            res = pthread_attr_init(&attr);

            if (res != 0)

            {

            printf("Create attribute failedn");

            exit(res);

            }

            /* 設(shè)置線程綁定屬性 */

            res = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

            /* 設(shè)置線程分離屬性 */

            res += pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

            if (res != 0)

            {

            printf("Setting attribute failedn");

            exit(res);

            }

            res = pthread_create(&thread, &attr, thrd_func, NULL);

            if (res != 0)

            {

            printf("Create thread failedn");

            exit(res);

            }

            /* 釋放線程屬性對象 */

            pthread_attr_destroy(&attr);

            printf("Create tread successn");

            while(!finish_flag)

            {

            printf("Waiting for thread to finish...n");

            sleep(2);

            }

            return 0;

            }

            接下來可以在線程運(yùn)行前后使用“free”命令查看內(nèi)存的使用情況。以下是運(yùn)行結(jié)果:

            $ ./thread_attr

            Create tread success

            Waiting for thread to finish...

            Thread is starting

            Waiting for thread to finish...

            Thread : job 0 delay = 3

            Waiting for thread to finish...

            Thread : job 1 delay = 2

            Waiting for thread to finish...

            Waiting for thread to finish...

            Waiting for thread to finish...

            Waiting for thread to finish...

            Thread : job 2 delay = 9

            Thread finished

            /* 程序運(yùn)行之前 */

            $ free

            total used free shared buffers cached

            Mem: 255556 191940 63616 10 5864 61360

            -/+ buffers/cache: 124716 130840

            Swap: 377488 18352 359136

            /* 程序運(yùn)行之中 */

            $ free

            total used free shared buffers cached

            Mem: 255556 191948 63608 10 5888 61336

            -/+ buffers/cache: 124724 130832

            Swap: 377488 18352 359136

            /* 程序運(yùn)行之后 */

            $ free

            total used free shared buffers cached

            Mem: 255556 191940 63616 10 5904 61320

            -/+ buffers/cache: 124716 130840

            Swap: 377488 18352 359136

            可以看到,線程在運(yùn)行結(jié)束后就收回了系統(tǒng)資源,并釋放內(nèi)存。

          linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

          linux相關(guān)文章:linux教程


          塵埃粒子計數(shù)器相關(guān)文章:塵埃粒子計數(shù)器原理

          上一頁 1 2 3 4 下一頁

          關(guān)鍵詞: Linux 多線程 互斥

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉