pthread 函数参数的范围
scope of pthread function arguments
这是pthread_setschedparam的签名:
#include <pthread.h>
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
这段代码会导致意外行为吗:
void schedule(const thread &t, int policy, int priority) {
sched_param params;
params.sched_priority = priority;
pthread_setschedparam(t.native_handle(), policy, ¶ms);
}
完全不清楚参数的范围是否需要比单独的函数调用更广泛。当我看到一个接受指针的函数时,它表明(至少对我而言)它正在请求它的所有权。这个签名是不是设计得很糟糕? "sched_params params" 应该住在堆上吗?它是否需要比线程更长的时间才能保持有效?可以删除吗?
我不知道。
谢谢!
pthread_setschedparam
设置给定线程的调度策略。参数调用后不需要存活
如果最后一个参数的生命周期很重要(如你所说,如果 pthread_setschedparam
取得它的所有权),它就会被明确记录下来。但它不在 POSIX 文档 pthread_setschedparam .
中
它采用指针(而不是值)的可能原因是传递指针比传递指针更便宜 struct
。
When I see a function that takes in a pointer, it suggests (to me at least) that it's asking for ownership of it.
当我看到一个接受指针参数的函数时,我不会直接跳到那里,我认为你也不应该。尽管了解这种可能性很重要,而且您最好查找文档,但函数采用指针参数的原因有多种,其中包括:
- 函数通过参数接受数组。这肯定是最常见的原因。
- 函数想要修改调用者指定给它的对象(通过指针)。这可能是第二个最常见的原因。
- 该函数接受指向大尺寸或可能大尺寸的结构或联合的指针,以减轻函数调用开销
该函数接受指向结构或联合的指针,因为它符合接口约定,这些约定适用于不接受结构和联合作为参数的古代 C 编译器。这对于早期的 C 编译器来说是正常的,因为这是最初指定语言的方式:
[T]he only operations you can perform on a structure are take its address with &
and access one of its members. [... Structures] can not be passed to or returned from functions. [...] Pointers to structures do not suffer these limitations[.]
(Kernighan & Ritchie,C 编程语言,第 1st 版,第 6.2 节)
标准 C 没有这些限制,但在某些地方仍然可以感受到它们的影响。
该函数希望承担(通常是重新分配)释放指针指向的动态分配的 space 的责任,或者它打算以其他方式制作指针的副本,该副本在函数的 return,在列表的下方。如果一个函数打算做这些事情之一,那么我完全希望它的文档以某种方式表明这一点。
Is this signature just badly designed?
不,我认为它的设计是由我列表中的后两点中的一个或两个提示的。
Should "sched_params params" live on the heap?
我不认为这是一项要求。
Does it need to outlive the thread to stay valid? Can it be deleted?
我认为它不需要超过设置了属性的线程。除了我对接口的一般解释外,我在函数的 POSIX 规范的措辞中阅读了对该位置的(弱)支持:
The pthread_setschedparam()
function shall set the scheduling policy
and associated scheduling parameters for the thread whose thread ID is
given by thread to the policy and associated parameters provided in
policy
and param
, respectively.
(POSIX specification for pthread_setscheduleparam()
;已强调)
"provided in" 语言向我表明(再次,微弱地)函数使用指向结构的内容,而不是结构本身。
这是pthread_setschedparam的签名:
#include <pthread.h>
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
这段代码会导致意外行为吗:
void schedule(const thread &t, int policy, int priority) {
sched_param params;
params.sched_priority = priority;
pthread_setschedparam(t.native_handle(), policy, ¶ms);
}
完全不清楚参数的范围是否需要比单独的函数调用更广泛。当我看到一个接受指针的函数时,它表明(至少对我而言)它正在请求它的所有权。这个签名是不是设计得很糟糕? "sched_params params" 应该住在堆上吗?它是否需要比线程更长的时间才能保持有效?可以删除吗?
我不知道。
谢谢!
pthread_setschedparam
设置给定线程的调度策略。参数调用后不需要存活
如果最后一个参数的生命周期很重要(如你所说,如果 pthread_setschedparam
取得它的所有权),它就会被明确记录下来。但它不在 POSIX 文档 pthread_setschedparam .
它采用指针(而不是值)的可能原因是传递指针比传递指针更便宜 struct
。
When I see a function that takes in a pointer, it suggests (to me at least) that it's asking for ownership of it.
当我看到一个接受指针参数的函数时,我不会直接跳到那里,我认为你也不应该。尽管了解这种可能性很重要,而且您最好查找文档,但函数采用指针参数的原因有多种,其中包括:
- 函数通过参数接受数组。这肯定是最常见的原因。
- 函数想要修改调用者指定给它的对象(通过指针)。这可能是第二个最常见的原因。
- 该函数接受指向大尺寸或可能大尺寸的结构或联合的指针,以减轻函数调用开销
该函数接受指向结构或联合的指针,因为它符合接口约定,这些约定适用于不接受结构和联合作为参数的古代 C 编译器。这对于早期的 C 编译器来说是正常的,因为这是最初指定语言的方式:
[T]he only operations you can perform on a structure are take its address with
&
and access one of its members. [... Structures] can not be passed to or returned from functions. [...] Pointers to structures do not suffer these limitations[.](Kernighan & Ritchie,C 编程语言,第 1st 版,第 6.2 节)
标准 C 没有这些限制,但在某些地方仍然可以感受到它们的影响。
该函数希望承担(通常是重新分配)释放指针指向的动态分配的 space 的责任,或者它打算以其他方式制作指针的副本,该副本在函数的 return,在列表的下方。如果一个函数打算做这些事情之一,那么我完全希望它的文档以某种方式表明这一点。
Is this signature just badly designed?
不,我认为它的设计是由我列表中的后两点中的一个或两个提示的。
Should "sched_params params" live on the heap?
我不认为这是一项要求。
Does it need to outlive the thread to stay valid? Can it be deleted?
我认为它不需要超过设置了属性的线程。除了我对接口的一般解释外,我在函数的 POSIX 规范的措辞中阅读了对该位置的(弱)支持:
The
pthread_setschedparam()
function shall set the scheduling policy and associated scheduling parameters for the thread whose thread ID is given by thread to the policy and associated parameters provided inpolicy
andparam
, respectively.
(POSIX specification for pthread_setscheduleparam()
;已强调)
"provided in" 语言向我表明(再次,微弱地)函数使用指向结构的内容,而不是结构本身。