Linux 下C語言多線程編程--線程屬性

線程屬性

屬性結構

union pthread_attr_t{
  char __size[__SIZEOF_PTHREAD_ATTR_T];
  long int __align;
};

屬性值不能直接設置,必須使用相關函數進行操作,線程屬性主要包括:是否綁定,是否分離,堆棧地址,堆棧大小,優先級

初始化函數

函數原型

/* Initialize thread attribute *ATTR with default attributes
   (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
    no user-provided stack).  */
extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));

:該函數必須在pthread_create()之前調用
默認:非綁定,非分離,默認1M的堆棧,與父進程同樣級別的優先級

關於綁定

輕進程(內核進程)位於系統層與用戶層之間,系統對線程的分配、控制都是通過輕進程來實現的,一個輕進程可以控制一個或者多個線程
非綁定:默認情況下啓動的輕進程以及它所控制的線程都是由系統決定的,所以這就是屬於非綁定的
綁定:把某個線程指定到某個輕進程上面(被綁定的線程具有較高的響應速度)

分離屬性

非分離:原有的線程等待創建的線程結束,只有當pthread_join()收到返回值時,創建的線程纔算終止,才能釋放空間
分離:線程函數執行完以後馬上釋放空間

綁定函數

/* Return in *SCOPE the scheduling contention scope of *ATTR.  */
extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr,
				  int *__restrict __scope)
     __THROW __nonnull ((1, 2));

第一個參數是指向線程屬性結構的指針,
第二個參數是綁定的類型,有兩個值:PTHREAD_SCOPE_SYSTEM(綁定的),PTHREAD_SCOPE_PROCES(非綁定的)

分離函數

/* Set detach state attribute.  */
extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
					int __detachstate)
     __THROW __nonnull ((1));

第一個參數是指向線程屬性結構的指針,
第二個參數:PTHREAD_CREATE_DETACHED(分離線程),PTHREAD_CREATE_JOINABLE(非分離線程)

:一般用pthread_cond_timewait()函數讓線程等待一會,留出足夠的時間讓pthread_create()返回(被創建的線程中調用)

/* Wait for condition variable COND to be signaled or broadcast until
   ABSTIME.  MUTEX is assumed to be locked before.  ABSTIME is an
   absolute time specification; zero is the beginning of the epoch
   (00:00:00 GMT, January 1, 1970).
   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
				   pthread_mutex_t *__restrict __mutex,
				   const struct timespec *__restrict __abstime)
     __nonnull ((1, 2, 3));

優先級

/* Data structure to describe a process' schedulability.  */
struct sched_param{
  int sched_priority;
};

/* Return in *PARAM the scheduling parameters of *ATTR.  */
extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr,
				       struct sched_param *__restrict __param)
     __THROW __nonnull ((1, 2));

/* Set scheduling parameters (priority, etc) in *ATTR according to PARAM.  */
extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
				       const struct sched_param *__restrict
				       __param) __THROW __nonnull ((1, 2));

結構體裏面使用一個int存放線程優先級,通過上面兩個函數設置和獲取線程優先級

Demo

#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

static void *test_thread_fun(void *arg) {
    while (1) {
        //在這裏添加需要執行的代碼
        fprintf(stderr, "%s(): thread is running!\n", __FUNCTION__);
        sleep(1);
    }

    fprintf(stderr, "%s(): thread terminated!\n", __FUNCTION__);
}

int main(int argc, char **argv) {
    pthread_attr_t attr;
    pthread_t pid;
    struct sched_param param;
    int i = 20;

    pthread_attr_init(&attr);
    pthread_attr_getschedparam(&attr, &param);
    param.sched_priority = i;

    pthread_attr_setschedparam(&attr, &param);
    //創建一個非綁定,非分離,優先級爲20的線程
    pthread_create(&pid, &attr, test_thread_fun, NULL);
    sleep(20);
    pthread_kill(pid);
    pthread_join(pid,NULL);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章