POSIX之Thread attribute

2022-12-05 15:56:04 浏览数 (2)

创建Thread时,需要通过参数指定attribute,如果参数为NULL则使用默认值。或者使用pthread_attr_init()初始化一个attribute对象

代码语言:javascript复制
/*
 * 初始化线程attribute.
 *   设置栈地址NULL, 表示- 由系统自动分配.
 *   设置栈尺寸0, 表示使用(20*1024).
 *   设置PTHREAD_CREATE_JOINABLE, 表示新线程支持与进程中其他线程同步
 *   设置线程间竞争CPU的范围为PTHREAD_SCOPE_SYSTEM
 *   设置任务名为NULL, 表示使用"pthr" 编号.
 *   设置任务选项为VX_FP_TASK, 表示支持浮点
 *   设置调度继承为PTHREAD_INHERIT_SCHED, 表示继承父继承的策略
 *   设置调度策略为SCHED_OTHER, 表示使用VxWorks的调度策略
 *   设置优先级为127
 */
int pthread_attr_init(pthread_attr_t *pAttr);
/* 标记attribute的状态为destroyed */
int pthread_attr_destroy(pthread_attr_t *pAttr);

可以使用相应的get/set函数来查询/设置attribute的值

代码语言:javascript复制
/*
 * 设置线程栈的低地址
 * 这个地址可由malloc()或memPartAlloc()获得
 * 必须在线程终止后, 显式free
 */
int pthread_attr_setstackaddr
    (
    pthread_attr_t *pAttr,
    void *pStackaddr
    );
/* 查询线程栈的低地址 */
int pthread_attr_getstackaddr
    (
    const pthread_attr_t *pAttr,
    void **ppStackaddr
    );
/* 设置线程的栈尺寸, 不低于4096 */
int pthread_attr_setstacksize
    (
    pthread_attr_t *pAttr,
    size_t stacksize
    );
/* 查询线程的栈尺寸 */
int pthread_attr_getstacksize
    (
    const pthread_attr_t *pAttr,
    size_t *pStacksize
    );
/* RTP only */
/* 设置线程栈的低地址和尺寸 */
int pthread_attr_setstack
    (
    pthread_attr_t *pAttr,
    void *pStackaddr,
    size_t stacksize
    );
/* RTP only */
/* 查询线程栈的低地址和尺寸 */
int pthread_attr_getstack
    (
    const pthread_attr_t *pAttr,
    void **ppStackaddr,
    size_t *pStackSize
    );
/*
 * 设置新线程是否支持与进程中其它线程同步
 * 参数为PTHREAD_CREATE_JOINABLE或PTHREAD_CREATE_DETACHED
 */
int pthread_attr_setdetachstate
    (
    pthread_attr_t *pAttr,
    int detachstate
    );
/* 查询新线程是否支持与进程中其它线程同步 */
int pthread_attr_getdetachstate
    (
    const pthread_attr_t *pAttr,
    int *pDetachstate
    );

/* 设置线程间竞争CPU的范围, 仅支持PTHREAD_SCOPE_SYSTEM */
int pthread_attr_setscope
    (
    pthread_attr_t *pAttr,
    int contentionScope
    );

/* 查询线程间竞争CPU的范围, 仅支持PTHREAD_SCOPE_SYSTEM */
int pthread_attr_getscope
    (
    const pthread_attr_t *pAttr,
    int *pContentionScope
    );
/* 设置VxWorks任务的名字 */
int pthread_attr_setname
    (
    pthread_attr_t *pAttr,
    char *name
    );
/* 查询VxWorks任务的名字 */
int pthread_attr_getname
    (
    pthread_attr_t *pAttr,
    char **name
    );
/* 设置VxWorks任务的选项 */
int pthread_attr_setopt
    (
    pthread_attr_t *pAttr,
    int options
    );
/* 查询VxWorks任务的选项 */
int pthread_attr_getopt
    (
    pthread_attr_t *pAttr,
    int *pOptions
    );
/*
 * 设置继承的属性, 可用值为:
 *   PTHREAD_INHERIT_SCHED - 从父进程继承
 *   PTHREAD_EXPLICIT_SCHED - 由参数指定
*/
int pthread_attr_setinheritsched
    (
    pthread_attr_t *pAttr,
    int inheritsched
    );

/* 查询继承的属性 */
int pthread_attr_getinheritsched
    (
    const pthread_attr_t *pAttr,
    int *pInheritsched
    );
/*
 * 设置调度策略, 可用值为:
 *   SCHED_RR - 时间片调度
 *   SCHED_FIFO - 先进先出调度
 *   SCHED_OTHER - 使用VxWorks调度
 */
int pthread_attr_setschedpolicy
    (
    pthread_attr_t *pAttr,
    int policy
    );
/* 查询调度策略 */
int pthread_attr_getschedpolicy
    (
    const pthread_attr_t *pAttr,
    int *pPolicy
    );

/*
 * 设置线程优先级
 *   sched_param.sched_priority
 * 优先级在PTHREAD_EXPLICIT_SCHED时有效
 */
int pthread_attr_setschedparam
    (
    pthread_attr_t *pAttr,
    const struct sched_param *pParam
    );

/* 查询线程优先级 */
int pthread_attr_getschedparam
    (
    const pthread_attr_t *pAttr,
    struct sched_param *pParam
    );
/* RTP only */
/*
 * 设置线程栈的guard尺寸
 * 未设置栈地址时生效
 */
int pthread_attr_setguardsize
    (
    pthread_attr_t *pAttr,
    size_t guardsize
    );
/* RTP only */
/* 查询线程栈的guard尺寸 */
int pthread_attr_getguardsize
    (
    const pthread_attr_t *pAttr,
    size_t *pGuardsize
    );

执行一下这些get,看看效果

代码语言:javascript复制
/*
 * 版权所有  公众号  VxWorks567
 * RTP
 */
#include <stdio.h>
#include <taskLib.h>
#include <pthread.h>

int main()
{
    pthread_attr_t attr;
    int data;
    struct sched_param schedData;

    pthread_attr_init(&attr);

    pthread_attr_getstackaddr(&attr, (void **)&data);
    printf("nstackaddr:t0x%xn", data);

    pthread_attr_getstacksize(&attr, (size_t *)&data);
    printf("stacksize:t%#xn", data);

    pthread_attr_getdetachstate(&attr, (int *)&data);
    printf("detach:tt%sn",
        (data==PTHREAD_CREATE_DETACHED)?
        "PTHREAD_CREATE_DETACHED":
        "PTHREAD_CREATE_JOINABLE");

    pthread_attr_getscope(&attr, (int *)&data);
    printf("scope:tt%sn",
        (data==PTHREAD_SCOPE_SYSTEM)?
        "PTHREAD_SCOPE_SYSTEM":"ERROR");

    pthread_attr_getname(&attr, (char **)&data);
    printf("name:tt%sn",
        (*(char *)data==0)?"NULL":(char *)data);

    pthread_attr_getopt(&attr, (int *)&data);
    printf("option:tt%sn",
        (data==VX_FP_TASK)?"VX_FP_TASK":"others");

    pthread_attr_getinheritsched(&attr, (int *)&data);
    printf("inherit:t%sn",
        (data==PTHREAD_INHERIT_SCHED)?
        "PTHREAD_INHERIT_SCHED":
        "PTHREAD_EXPLICIT_SCHED");

    pthread_attr_getschedpolicy(&attr, (int *)&data);
    printf("policy:tt%sn",
        (data==SCHED_OTHER)?"SCHED_OTHER":
        ((data==SCHED_FIFO)?"SCHED_FIFO":"SCHED_RR"));

    pthread_attr_getschedparam(&attr, &schedData);
    printf("priority:t%dn",
        schedData.sched_priority);

    pthread_attr_getguardsize(&attr, (size_t *)&data);
    printf("guardsize:t%#xnn", data);

    pthread_attr_destroy(&attr);
    return OK;
    }

还有几个API可以在创建线程后,动态使用

代码语言:javascript复制
/* 查询线程<thread>的优先级以及调度策略是SCHED_FIFO或SCHED_RR */
int pthread_getschedparam
    (
    pthread_t thread,
    int *pPolicy,
    struct sched_param *pParam
    );

/*
 * 设置线程<thread>的优先级
 * 调度策略<policy>可以是SCHED_OTHER,
 * 如果是SCHED_FIFO或SCHED_RR, 则需求与当前策略一致
 */
int pthread_setschedparam
    (
    pthread_t thread,
    int policy,
    const struct sched_param *pParam
    );

/* RTP only */
/* 设置线程<thread>的优先级 */
int pthread_setschedprio
    (
    pthread_t thread,
    int prio
    );

/* 查询调度策略为<policy>时的最高优先级 */
int sched_get_priority_max(int policy);
/* 查询调度策略为<policy>时的最低优先级 */
int sched_get_priority_min(int policy);
代码语言:javascript复制
/*
 * 版权所有  公众号  VxWorks567
 * RTP
 */
#include <stdio.h>
#include <pthread.h>

static void *subThread(void *arg)
{
    return NULL;
    }

int main()
{
    pthread_t id;
    int data;
    struct sched_param schedData;

    pthread_create(&id, NULL, subThread, NULL);

    pthread_getschedparam(id, &data, &schedData);
    printf("nschedpolicy:t%sn",
        (data==SCHED_OTHER)?"SCHED_OTHER":
        ((data==SCHED_FIFO)?"SCHED_FIFO":"SCHED_RR"));
    printf("Posix priority:t%dn", schedData.sched_priority);

    printf("priority_max SCHED_FIFO:%dn",
        sched_get_priority_max(SCHED_FIFO));
    printf("priority_max SCHED_RR:t%dn",
        sched_get_priority_max(SCHED_RR));
    printf("priority_max SCHED_OTHER:%dn",
        sched_get_priority_max(SCHED_OTHER));

    printf("priority_min SCHED_FIFO:%dn",
        sched_get_priority_min(SCHED_FIFO));
    printf("priority_min SCHED_RR:t%dn",
        sched_get_priority_min(SCHED_RR));
    printf("priority_min SCHED_OTHER:%dn",
        sched_get_priority_min(SCHED_OTHER));

    pthread_join(id, NULL);
    return OK;
    }

从max和min可以看到,Posix的优先级范围,与VxWorks的优先级相反,即Posix的255为最高优先级,而VxWorks的0为最高。那为什么rtpSp()启动的RTP里的Thread使用35的优先级呢?

0 人点赞