pthread_create返回值

今晚在跑之前写的一个创建新线程的代码,发现:执行设置系统调度策略以及调度参数相关的参数后,调用pthread_create生成新的任务时,得到返回值为1的情况:

    pthread_attr_init(&attr);
    if (desc->stack_size != 0)
    {
        pthread_attr_setstacksize(&attr, desc->stack_size);
    }
    pthread_attr_getinheritsched(&attr, &inher);
    if (inher != PTHREAD_EXPLICIT_SCHED)
    {
        log_warn("change sched!\n");
        inher = PTHREAD_EXPLICIT_SCHED;
    }
    pthread_attr_setinheritsched(&attr, inher);
    // 设置线程调度策略
    policy = SCHED_FIFO;
    pthread_attr_setschedpolicy(&attr, policy);
    // 设置调度参数
    param.sched_priority = desc->prio;
    pthread_attr_setschedparam(&attr, &param);

    ret = pthread_create(tinfo, &attr, desc->fn, desc->arg);
    // TODO: return code = 1 ???
    if (ret != 0)
    {
        log_err("task create err code = (%d)\n", ret);
        pthread_attr_destroy(&attr);
        free(tinfo);
        return NULL;
    }

打印信息:

Work in host mode!
Upgrade file = defines.mk
comm rule = 0x10
set port = /dev/ttyUSB0
Success open /dev/ttyUSB0
change sched!
task create err code = (1)
Create Thread phrase core failed!
comm core init failed!
comm init err!

但是从 http://www.man7.org/linux/man-pages/man3/pthread_create.3.html

查找相关返回值信息,找到了返回值为1的情况:

RETURN VALUE         top

       On success, pthread_create() returns 0; on error, it returns an error
       number, and the contents of *thread are undefined.

ERRORS         top

       EAGAIN Insufficient resources to create another thread.

       EAGAIN A system-imposed limit on the number of threads was
              encountered.  There are a number of limits that may trigger
              this error: the RLIMIT_NPROC soft resource limit (set via
              setrlimit(2)), which limits the number of processes and
              threads for a real user ID, was reached; the kernel's system-
              wide limit on the number of processes and threads,
              /proc/sys/kernel/threads-max, was reached (see proc(5)); or
              the maximum number of PIDs, /proc/sys/kernel/pid_max, was
              reached (see proc(5)).

       EINVAL Invalid settings in attr.

       EPERM  No permission to set the scheduling policy and parameters
              specified in attr.

其中的ERRORS = EPERM的定义,在 erron.h 头文件中的定义,就是1,从解释看,就是说没有权限来设置调度策略及参数定义。

所以,这个代码涉及到修改调度策略,需要在ROOT权限下来执行。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值