天天看點

Linux多線程實踐(3) --線程屬性

初始化/銷毀線程屬性

線程分離屬性

參數說明:

The following values may be specified in detachstate:

   PTHREAD_CREATE_DETACHED

Threads that are created using attr will be created in a detached state.

   PTHREAD_CREATE_JOINABLE

Threads that are created using attr will be created in a joinable state.

The  default  setting  of  the  detach  state  attribute  in  a  newly initialized 

threadattributes object is PTHREAD_CREATE_JOINABLE.

The pthread_attr_getdetachstate() returns  the  detach  state  attribute  of  the  

threadattributes object attr in the buffer pointed to by detachstate.

線程棧大小

DESCRIPTION

  The pthread_attr_setstacksize() function sets the stack  size  attribute  

of  the  threadattributes object referred to by attr to the value specified 

in stacksize.(一般情況下該值我們設定為0,使用系統預設設定的線程棧大小,否則可能會引起程式的可移植性的問題)       The  stack  size  attribute determines the minimum size (in bytes) that will 

be allocatedfor threads created using the thread attributes object attr.

  The pthread_attr_getstacksize() function returns the stack size attribute of  

the  threadattributes object referred to by attr in the buffer pointed to by stacksize.

線程棧溢出保護區大小

線程競争範圍(程序範圍内的競争 or 系統範圍内的競争)

contentionscope說明:

   PTHREAD_SCOPE_SYSTEM

The  thread  competes for resources with all other threads in all processes on 

the system that are in the same scheduling allocation domain (a group of one  or  more processors).   

PTHREAD_SCOPE_SYSTEM  threads are scheduled relative to one anotheraccording to their 

scheduling policy and priority.

   PTHREAD_SCOPE_PROCESS

The thread competes for resources with all other threads in the same process  thatwere    

also created with the PTHREAD_SCOPE_PROCESS contention scope.

  PTHREAD_SCOPE_PROCESS threads are scheduled  relative  to  other  threads  in  the process 

according to their scheduling policy and priority.  POSIX.1-2001 leaves it unspecified how these threads contend with other threads in other process  

on  the system  or  with  other  threads  in  the  same process that were created with the 

PTHREAD_SCOPE_SYSTEM contention scope.

線程排程政策

 The pthread_attr_setschedpolicy() function sets the scheduling policy  attribute  of  the thread  

attributes  object  referred  to  by attr to the value specified in policy.  This attribute determines  

the  scheduling  policy  of  a  thread  created  using  the  thread attributes object attr.

 The  supported  values  for  policy  are  SCHED_FIFO, SCHED_RR, and SCHED_OTHER, as below:

  SCHED_FIFO    a first-in, first-out policy(先進先出排程政策); 

  SCHED_RR      a round-robin policy(時間片輪轉排程算法);

  SCHED_OTHER   the standard round-robin time-sharing policy(線程一旦開始運作,直到被搶占或者直到線程阻塞或停止為止);

注意:

 In order for the policy setting made by pthread_attr_setschedpolicy() 

to have effect when calling pthread_create(3), the caller must use pthread_attr_setinheritsched(3)(見下) 

to set the inherit-scheduler attribute of the attributes object attr to PTHREAD_EXPLICIT_SCHED(新建立的程序繼承自己的排程屬性).

線程繼承的排程政策

The following values may be specified in inheritsched:

  PTHREAD_INHERIT_SCHED(繼承排程屬性)

Threads that are created using attr inherit scheduling attributes from the  

creating thread; the scheduling attributes in attr are ignored.

  PTHREAD_EXPLICIT_SCHED(指定自己的排程屬性)

Threads that are created using attr take their scheduling attributes from 

the values specified by the attributes object.

The default setting of the inherit-scheduler attribute  in  a  newly  initialized  thread attributes object is PTHREAD_INHERIT_SCHED.

線程排程參數(實際上我們一般隻關心一個參數:線程的優先級,預設為0)

線程的并發級别

說明:并發級别僅在N:M線程模型中有效,設定并發級别,給核心一個提示:表示提供給定級别數量的核心線程來映射使用者線程是高效的(僅僅是一個提示),預設為0, 核心按照預設的方式進行并發;

Linux多線程實踐(3) --線程屬性

說明:

綁定屬性:

  Linux中采用“一對一”的線程機制,也就是一個使用者線程對應一個核心線程。綁定屬性就是指一個使用者線程固定地配置設定給一個核心線程,因為CPU時間片的排程是面向核心線程(也就是輕量級程序)的,是以具有綁定屬性的線程可以保證在需要的時候總有一個核心線程與之對應。而與之對應的非綁定屬性就是指使用者線程和核心線程的關系不是始終固定的,而是由系統來控制配置設定的。 

分離屬性:

  分離屬性是用來決定一個線程以什麼樣的方式來終止自己。在非分離情況下,當一個線程結束時,它所占用的系統資源并沒有被釋放,也就是沒有真正的終止。隻有當pthread_join()函數傳回時,建立的線程才能釋放自己占用的系統資源。而在分離屬性情況下,一個線程結束時立即釋放它所占有的系統資源。這裡要注意的一點是,如果設定一個線程的分離屬性,而這個線程運作又非常快,那麼它很可能在pthread_create()函數傳回之前就終止了,它終止以後就可能将線程号和系統資源移交給其他的線程使用。

繼續閱讀