新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 多線程編程之:Linux線程編程

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

        作者: 時間:2014-10-17 來源:網絡 收藏

          9.2.3 線程屬性

        本文引用地址:http://www.104case.com/article/264053.htm

          (1)函數說明。

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

          n 綁定屬性。

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

          n 分離屬性。

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

          這些屬性的設置都是通過特定的函數來完成的,通常首先調用pthread_attr_init()函數進行初始化,之后再調用相應的屬性設置函數,最后調用pthread_attr_destroy()函數對分配的屬性結構指針進行清理和回收。設置綁定屬性的函數為pthread_attr_setscope(),設置線程分離屬性的函數為pthread_attr_setdetachstate(),設置線程優先級的相關函數為pthread_attr_getschedparam()(獲取線程優先級)和pthread_attr_setschedparam()(設置線程優先級)。在設置完這些屬性后,就可以調用pthread_create()函數來創建線程了。

          (2)函數格式。

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

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

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

          表9.12 pthread_attr_getschedparam()函數語法要點

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

          (3)使用實例。

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

          /*thread_attr.c*/

          #include

          #include

          #include

          #define REPEAT_NUMBER 3 /* 線程中的小任務數 */

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

          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);

          }

          /* 設置線程綁定屬性 */

          res = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

          /* 設置線程分離屬性 */

          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;

          }

          接下來可以在線程運行前后使用“free”命令查看內存的使用情況。以下是運行結果:

          $ ./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

          /* 程序運行之前 */

          $ free

          total used free shared buffers cached

          Mem: 255556 191940 63616 10 5864 61360

          -/+ buffers/cache: 124716 130840

          Swap: 377488 18352 359136

          /* 程序運行之中 */

          $ free

          total used free shared buffers cached

          Mem: 255556 191948 63608 10 5888 61336

          -/+ buffers/cache: 124724 130832

          Swap: 377488 18352 359136

          /* 程序運行之后 */

          $ free

          total used free shared buffers cached

          Mem: 255556 191940 63616 10 5904 61320

          -/+ buffers/cache: 124716 130840

          Swap: 377488 18352 359136

          可以看到,線程在運行結束后就收回了系統資源,并釋放內存。

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

        linux相關文章:linux教程


        塵埃粒子計數器相關文章:塵埃粒子計數器原理

        上一頁 1 2 3 4 下一頁

        關鍵詞: Linux 多線程 互斥

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 湛江市| 太谷县| 灵石县| 东莞市| 通江县| 鲁山县| 建湖县| 梁平县| 岐山县| 福清市| 南城县| 濮阳县| 遵义县| 曲靖市| 晋宁县| 晋州市| 农安县| 南涧| 盘山县| 玉山县| 偏关县| 三原县| 泊头市| 甘谷县| 合川市| 达拉特旗| 巩留县| 施秉县| 贵港市| 图们市| 裕民县| 东平县| 盈江县| 盱眙县| 连平县| 高尔夫| 沁源县| 烟台市| 桃江县| 秭归县| 广州市|