pthread_attr_setschedparam 和 pthread_setschedparam 有什么区别?

what's the difference between pthread_attr_setschedparam and pthread_setschedparam?

pthread_attr_setschedparam 和 pthread_setschedparam 有什么区别?我应该如何决定选择一个使用?

命名令人困惑,但它们的用途不同。

pthread_setschedparam用于设置线程的参数

pthread_attr_setschedparam用于设置线程参数的属性。

也就是说

  • 如果你想设置你必须使用的特定属性 pthread_attr_setschedparam
  • 如果你想为你拥有的胎面设置特定参数 使用 pthread_setschedparam

pthread_setschedparam() 为现有线程设置调度程序策略和调度程序参数。

pthread_attr_setschedparam()pthread_attr_setschedpolicy() 分别为 线程属性对象 (类型 pthread_attr_t)设置调度程序参数和调度程序策略。这将为随后使用该线程属性对象创建的任何新线程设置调度程序参数和调度程序策略(例如,您可以将线程属性对象作为 pthread_create() 的第二个参数传递)。

所以您要使用哪一个取决于您是要设置现有线程还是新线程的调度程序策略/参数。